提问者:小点点

unity C#中的漫游AI


我在试着创造一个游荡的人工智能

我正在使用unity standard assets第三人称人工智能

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

在这些地点之间巡逻

这是密码?

如何修改为巡逻?

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 target;                                    // target to aim for


        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();
            character = GetComponent();

            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
                character.Move(Vector3.zero, false, false);
        }


        public void SetTarget(Transform target)
        {
            this.target = target;
        }
    }
}

共1个答案

匿名用户

要让人工智能在两点之间巡逻,你需要定义第二个点,并改变人工智能的行为,当它到达第一个点时改变目标。目前,一旦它到达目标(即停止),它将简单地以零速度移动。

不需要过多地修改代码,您可以通过这样做将其扩展为在两个位置之间移动。

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如果太接近当前目标就改变目标。在顶部还有一个额外的转换定义(start)需要设置,还有一个boolean用来存储AI的方向。

这段代码还没有在Unity中测试过,所以可能需要一些修改,但它应该会给你正确的想法。