【问题标题】:Wandering AI in unity C#在 Unity C# 中漫游 AI
【发布时间】:2017-09-17 23:16:24
【问题描述】:

我正在尝试创建一个漫游的 AI

我正在使用统一标准资产第三人称 AI

但问题是人工智能只是移动到某个点,它不能

在这些点之间巡逻

这是代码?

如何修改它来巡逻?

使用系统; 使用 UnityEngine; 命名空间 UnityStandardAssets.Characters.ThirdPerson { [RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))] [RequireComponent(typeof (ThirdPersonCharacter))] 公共类 AICharacterControl:MonoBehaviour { 公共 UnityEngine.AI.NavMeshAgent 代理 { 获取;私人套装; } // 寻路所需的 navmesh 代理 公共 ThirdPersonCharacter 字符 { 获取;私人套装; } // 我们控制的角色 公共转换目标; // 要瞄准的目标 私人无效开始() { // 获取我们需要的对象上的组件(由于需要组件,因此不应为空,因此无需检查) 代理 = GetComponentInChildren(); 字符 = 获取组件(); agent.updateRotation = false; 代理.updatePosition = true; } 私人无效更新() { 如果(目标!= null) agent.SetDestination(target.position); 如果 (agent.remainingDistance > agent.stoppingDistance) character.Move(agent.desiredVelocity, false, false); 别的 character.Move(Vector3.zero, false, false); } 公共无效 SetTarget(转换目标) { this.target = 目标; } } }

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    要让AI在两点之间巡逻,你需要定义第二个点并改变AI的行为来改变目标,当它到达第一个点时。目前,它一旦到达目标(即停止)就会以零速度移动。

    无需过多修改代码,您可以通过执行以下操作将其扩展为在两个位置之间移动。

    using System;
    using UnityEngine;
    
    namespace UnityStandardAssets.Characters.ThirdPerson
    {
    
        [RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))]
        [RequireComponent(typeof (ThirdPersonCharacter))]
        public class AICharacterControl : MonoBehaviour
        {
            public UnityEngine.AI.NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
            public ThirdPersonCharacter character { get; private set; } // the character we are controlling
            public Transform start;
            public Transform end;
    
            private Transform target;
            private boolean forward = true;
    
            private void Start()
            {
                // get the components on the object we need ( should not be null due to require component so no need to check )
                agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
                character = GetComponent<ThirdPersonCharacter>();
    
                agent.updateRotation = false;
                agent.updatePosition = true;
            }
    
    
            private void Update()
            {
                if (target != null)
                    agent.SetDestination(target.position);
    
                if (agent.remainingDistance > agent.stoppingDistance)
                {
                    character.Move(agent.desiredVelocity, false, false);
                }
                else
                {
                    SetTarget(forward ? start : end);
                    forward = !forward;
                }
            }
    
            public void SetTarget(Transform target)
            {
                this.target = target;
            }
        }
    }
    

    如您所见,我修改了Update() 以告诉 AI 如果目标太接近当前目标则更改目标。顶部还有一个额外的 Transform 定义 (start) 需要设置,boolean 用于存储 AI 前进的方向。

    此代码尚未在 Unity 中进行测试,因此可能需要进行一些修改,但它应该可以为您提供正确的想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多