我正在尝试创建一个游荡的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 回答
要在两点之间进行AI巡检,您需要定义第二个点并更改AI的行为,以便在到达第一个点时更改目标 . 目前,一旦达到目标(即停止),它将简单地以零速度移动 .
在不修改代码的情况下,可以通过执行类似操作来扩展它以在两个位置之间移动 .
正如你所看到的,我已经修改了
Update()
,告诉AI如果它变得太接近当前目标就改变目标 . 顶部还有一个需要设置的额外变换定义(start
),boolean
用于存储AI的方向 .此代码尚未在Unity中进行过测试,因此可能需要进行一些修改,但它应该为您提供正确的想法 .