void ANPC::Tick(float DeltaTime) { Super::Tick(DeltaTime); if (FollowPlayer) { FVector PlayerLocation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation(); FVector Direction = (PlayerLocation - GetActorLocation()).GetSafeNormal(); AddMovementInput(Direction, FollowSpeed * DeltaTime); } }
public class PatrolAI : MonoBehaviour { public Transform[] Waypoints; public float Speed = 5f; private int _currentWaypointIndex = 0; private NavMeshAgent _navMeshAgent; private void Start() { _navMeshAgent = GetComponentThe package/library used in these examples is Unreal Engine 4 and Unity respectively.(); _navMeshAgent.SetDestination(Waypoints[_currentWaypointIndex].position); } private void Update() { if (_navMeshAgent.remainingDistance < 0.5f) { _currentWaypointIndex = (_currentWaypointIndex + 1) % Waypoints.Length; _navMeshAgent.SetDestination(Waypoints[_currentWaypointIndex].position); } } }