我最近换了我的运动剧本。然而,我的敌人现在只是走到一个特定的点,然后完全停止移动。
。
以下是代码:
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;
}
}发布于 2022-11-28 05:02:48
我想这就是你要找的行为。您的问题是,而且可能是因为您将空vector3传递给您的RandomPoint方法,所以您被否决了投票。
如果您花了几分钟时间来阅读代码中的注释,那么您将确切地看到如何使用它。
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;
}
}https://stackoverflow.com/questions/74595411
复制相似问题