c# - 玩家没有移动到所有航点。
问题描述
敌方角色没有移动到第三个航路点。移动到航路点 2 后,它会停止并播放空闲动画。角色上有一个 NavMeshAgent,看起来目的地到达事件在他到达航点时没有被触发。如果有人遇到过这样的情况,我将不胜感激任何可能的信息。几个小时以来,我一直试图找出问题所在,并且开始认为这可能不是任何脚本。
这是航点控制器
using UnityEngine;
using UnityEngine.AI;
public class WaypointController : MonoBehaviour {
Waypoints[] waypoints;
public Transform target;
//NavMeshPath path;
int currentWaypointIndex = -1;
//private NavMeshAgent agent;
//EnemyCharacter enemy;
public event System.Action<Waypoints> OnWaypointChanged;
// Use this for initialization
void Awake () {
waypoints = GetWaypoints();
}
public void SetNextWaypoint() {
if (currentWaypointIndex != waypoints.Length)
currentWaypointIndex++;
if (currentWaypointIndex == waypoints.Length)
currentWaypointIndex = 0;
if (OnWaypointChanged != null)
OnWaypointChanged(waypoints[currentWaypointIndex]);
//Debug.Log("OnWaypointChanged == null: " + (OnWaypointChanged == null));
//Debug.Log("OnWaypointChanged != null: " + (OnWaypointChanged != null));
}
Waypoints[] GetWaypoints()
{
return GetComponentsInChildren<Waypoints>();
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Vector3 previousWaypoint = Vector3.zero;
foreach (var waypoint in GetWaypoints())
{
Vector3 waypointPosition = waypoint.transform.position;
Gizmos.DrawWireSphere(waypointPosition, .2f);
if (previousWaypoint != Vector3.zero)
Gizmos.DrawLine(previousWaypoint, waypointPosition);
previousWaypoint = waypointPosition;
}
}
}
这是 EnemyPatrolPoints 脚本
using UnityEngine;
[RequireComponent(typeof(AI_PathFinder))]
public class EnemyPatrolPoints : MonoBehaviour {
[SerializeField]
WaypointController waypointController;
[SerializeField]
float waitTimeMin;
[SerializeField]
float waitTimeMax;
AI_PathFinder pathfinder;
private void Start()
{
waypointController.SetNextWaypoint();
}
private void Awake()
{
pathfinder = GetComponent<AI_PathFinder>();
pathfinder.OnDestinationReached += Pathfinder_OnDestinationReached;
waypointController.OnWaypointChanged += WaypointController_OnWaypointChanged;
}
private void WaypointController_OnWaypointChanged(Waypoints waypoint)
{
pathfinder.SetTarget(waypoint.transform.position);
print("waypoint changed");
}
private void Pathfinder_OnDestinationReached()
{
SealForce_GameManager.Instance.Timer.Add(waypointController.SetNextWaypoint, Random.Range(waitTimeMin, waitTimeMax));
print("destination reached");
}
}
这是 AI Pathfinder 脚本`
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class AI_PathFinder : MonoBehaviour
{
[HideInInspector]
public NavMeshAgent agent;
public EnemyPatrolPoints enemyPatrolPoints;
[SerializeField] float distanceRemainingThreshold;
bool m_destinationReached;
bool destinationReached
{
get
{ return m_destinationReached; }
set
{
m_destinationReached = value;
if (m_destinationReached)
{
if (OnDestinationReached != null)
OnDestinationReached();
}
}
}
public event System.Action OnDestinationReached;
void Start()
{
agent = GetComponent<NavMeshAgent>();
//enemyPatrolPoints = GetComponent<EnemyPatrolPoints>();
}
public void SetTarget(Vector3 target)
{
agent.SetDestination(target);
}
void Update()
{
if (destinationReached)
return;
if (agent.remainingDistance < distanceRemainingThreshold)
destinationReached = true;
}
}
解决方案
线条
if (agent.remainingDistance < distanceRemainingThreshold)
destinationReached = true;
只要destinationReached
是true
因为
if (destinationReached)
return;
您将其设置为true
在到达第一个航路点之后,然后再将其重置为,false
因此您Update
将来总是会被跳过。
您应该将其添加到
public void SetTarget(Vector3 target)
{
agent.SetDestination(target);
destinationReached = false;
}
推荐阅读
- dgraph - dgraph 什么时候支持 Gremlin
- css - firefox 在点击下拉菜单时遇到问题
- java - Ubuntu VDS 上的 java selenium 测试。该网站未完全加载
- python - 查找在循环中创建的按钮 ID
- node.js - 带有 React 和 OIDC 身份验证的 Electron 在 DEV 工作,但在 PROD 失败
- python - 如何在 Python numpy 中将行和列变成变量?
- git - GitHub存储库中的文件在哪里?
- ruby-on-rails - Ruby on Rails - 参数丢失或值为空
- azure - 我们应该将什么服务连接添加到 Azure 管道中的“调用 REST API:POST”
- r - 用于删除组内值的 R 代码