这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

53 行
1.1 KiB

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PathwayMovementAction : NPCMovementAction
{
private NavMeshAgent _agent;
private bool _isActiveAgent;
private List<WaypointData> _wayppoints;
private int _wayPointIndex;
private float _roamingSpeed;
public PathwayMovementAction(
PathwayConfigSO config, NavMeshAgent agent)
{
_agent = agent;
_isActiveAgent = _agent != null && _agent.isActiveAndEnabled && _agent.isOnNavMesh;
_wayPointIndex = 0;
_roamingSpeed = config.Speed;
_wayppoints = config.Waypoints;
}
public override void OnUpdate()
{
}
public override void OnStateEnter()
{
if (_isActiveAgent)
{
_agent.speed = _roamingSpeed;
_agent.isStopped = false;
_agent.SetDestination(GetNextDestination());
}
}
public override void OnStateExit()
{
}
private Vector3 GetNextDestination()
{
Vector3 result = _agent.transform.position;
if (_wayppoints.Count > 0)
{
_wayPointIndex = (_wayPointIndex + 1) % _wayppoints.Count;
result = _wayppoints[_wayPointIndex].waypoint;
}
return result;
}
}