在Unity中漫游的人工智能

7

我想创建一个漫游AI。我正在使用Unity标准资产第三人称AI,但问题是AI只移动到某个特定点,并且无法在这些点之间巡逻。

以下是代码:

using System;
using UnityEngine;

namespace UnityStandardAssets.Characters.ThirdPerson
{
    [RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))]
    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class AICharacterControl : MonoBehaviour
    {
        // the navmesh agent required for the path finding
        public UnityEngine.AI.NavMeshAgent agent { get; private set; }
        // the character we are controlling                    
        public ThirdPersonCharacter character { get; private set; }
        // target to aim for
        public Transform target;

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

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

我该如何修改它以进行巡逻?

1个回答

10

要使AI在两个点之间巡逻,您需要定义第二个点并更改AI的行为,以便它在到达第一个点时更换目标。目前,一旦AI到达目标(即停止),它将简单地以零速度移动。

不需要太多修改您的代码,您可以通过像这样做来扩展其在两个位置之间移动的能力。

using System;
using UnityEngine;

namespace UnityStandardAssets.Characters.ThirdPerson
{
    [RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))]
    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class AICharacterControl : MonoBehaviour
    {
        // the navmesh agent required for the path finding
        public UnityEngine.AI.NavMeshAgent agent { get; private set; }
        // the character we are controlling
        public ThirdPersonCharacter character { get; private set; } 
        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)需要设置,以及一个用于存储AI方向的boolean变量。

这段代码尚未在Unity中进行测试,可能需要进行一些修改,但它应该能够给您正确的想法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接