【Unity,C#】哨兵点位循迹模板代码

哨兵点位循迹模板代码


效果

在这里插入图片描述

配置

在这里插入图片描述

代码

public class Sentry : MonoBehaviour
{
    private NavMeshAgent _navMeshAgent;
    private int _currentWaypointIndex = 0;

    public Transform[] waypoints;


    private void Start()
    {
        _navMeshAgent = GetComponent<NavMeshAgent>();
        _navMeshAgent.SetDestination(waypoints[_currentWaypointIndex].position);
    }

    private void Update()
    {
        if (_navMeshAgent.remainingDistance <= _navMeshAgent.stoppingDistance)
        {
            _currentWaypointIndex = (_currentWaypointIndex + 1) % waypoints.Length;
            _navMeshAgent.SetDestination(waypoints[_currentWaypointIndex].position);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42473228/article/details/126059619