在团结中徘徊AI#

时间:2017-04-21 05:17:46

标签: c# unity3d

我试图创建一个游荡的AI

我使用统一标准资产第三人称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 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 个答案:

答案 0 :(得分:5)

要在两点之间进行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如果它太接近当前目标则更改目标。在顶部还需要设置一个额外的转换定义(start),并且boolean用于存储AI的走向。

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