敌人一旦到达某个点就会停止,是否有办法调整它,让它停止这样做?



我最近替换了我的移动脚本。然而,我的敌人现在只是走到某一点,然后完全停止移动。

  • 我想知道我是否可以添加或更改任何代码,使其连续移动,而不是追逐玩家?

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class StalkerAI : MonoBehaviour
{
public NavMeshAgent agent;
public Transform Player;
public float range; //radius of sphere
public Transform centrePoint; //centre of the area the agent wants to move around in
//instead of centrePoint you can set it as the transform of the agent if you don't care about a specific area

void Start()
{
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
float distVal = 5.0f;
float dist = Vector3.Distance(agent.transform.position, Player.position);
if (dist <= distVal)
{
agent.SetDestination(Player.position);
}
else
{
if (agent.remainingDistance <= agent.stoppingDistance) //done with path
{
Vector3 point;
if (RandomPoint(centrePoint.position, range, out point)) //pass in our centre point and radius of area
{
Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f); //so you can see with gizmos
agent.SetDestination(point);
}
}
}
}
bool RandomPoint(Vector3 center, float range, out Vector3 result)
{
Vector3 randomPoint = center + Random.insideUnitSphere * range; //random point in a sphere 
NavMeshHit hit;
if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas)) //documentation: https://docs.unity3d.com/ScriptReference/AI.NavMesh.SamplePosition.html
{
//the 1.0f is the max distance from the random point to a point on the navmesh, might want to increase if range is big
//or add a for loop like in the documentation
result = hit.position;
return true;
}
result = Vector3.zero;
return false;
}
}

我认为这是你正在寻找的行为。你的问题是,可能你被否决的原因是因为你传递了一个空向量给你的随机点方法。

如果你花几分钟读一下代码中的注释,你就会明白如何使用它了。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class StalkerAI : MonoBehaviour
{
public NavMeshAgent agent;
public Transform Player;
public float range; //radius of sphere
public Vector3 centrePoint; //centre of the area the agent wants to move around in
//instead of centrePoint you can set it as the transform of the agent if you don't care about a specific area

void Start()
{
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
float distVal = 5.0f;
float dist = Vector3.Distance(agent.transform.position, Player.position);
if (dist <= distVal)
{
agent.SetDestination(Player.position);
}
else
{
if (agent.remainingDistance <= agent.stoppingDistance) //done with path
{
centrePoint = transform.position;
if (RandomPoint(centrePoint, range, out centrePoint)) //pass in our centre point and radius of area
{
Debug.DrawRay(centrePoint, Vector3.up, Color.blue, 1.0f); //so you can see with gizmos
agent.SetDestination(centrePoint);
}
}
}
}
bool RandomPoint(Vector3 center, float range, out Vector3 result)
{
Vector3 randomPoint = center + Random.insideUnitSphere * range; //random point in a sphere
NavMeshHit hit;
if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas)) //documentation: https://docs.unity3d.com/ScriptReference/AI.NavMesh.SamplePosition.html
{
//the 1.0f is the max distance from the random point to a point on the navmesh, might want to increase if range is big
//or add a for loop like in the documentation
result = hit.position;
return true;
}
result = Vector3.zero;
return false;
}
}

相关内容

最新更新