The NavMeshAgent Move() function is a method in the UnityEngine.AI package library that allows the game object controlled by the NavMeshAgent to move towards a specified target location.
Example 1:
NavMeshAgent agent = GetComponent(); Vector3 target = new Vector3(10, 0, 10); // set the destination target agent.SetDestination(target); // move towards the target
In this example, the NavMeshAgent component is retrieved from the game object, and a target Vector3 position is set. The SetDestination() function is then used to move the game object towards the target using the NavMeshAgent.
Example 2:
NavMeshAgent agent = GetComponent(); float moveSpeed = 5f; Vector3 currentPosition = agent.transform.position; Vector3 target = new Vector3(0, 0, 10); // set the destination target Vector3 moveDirection = (target - currentPosition).normalized; // normalize the distance vector to get the direction Vector3 movePosition = currentPosition + moveDirection * moveSpeed * Time.deltaTime; // calculate the next position using deltaTime agent.Move(movePosition - currentPosition); // move the game object towards the target
In this example, the NavMeshAgent component is retrieved from the game object, and a move speed value is set. The current position of the game object is retrieved and a target Vector3 position is set. The distance vector between the current position and target position is normalized to get the direction. The next position is then calculated based on the move speed and deltaTime. Finally, the game object is moved towards the target using the Move() function.
C# (CSharp) UnityEngine.AI NavMeshAgent.Move - 20 examples found. These are the top rated real world C# (CSharp) examples of UnityEngine.AI.NavMeshAgent.Move extracted from open source projects. You can rate examples to help us improve the quality of examples.