AI Navigation
In this example, we make use of the new NavMeshCharacter
component introduced in version 1.4.0 to implement a typical click-to-move movement.
public class ClickToMove : MonoBehaviour
{
public Camera mainCamera;
public Character character;
public LayerMask groundMask;
private NavMeshCharacter _navMeshCharacter;
private void Awake()
{
_navMeshCharacter = character.GetComponent<NavMeshCharacter>();
}
private void Update()
{
if (Input.GetMouseButton(0))
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hitResult, Mathf.Infinity, groundMask))
_navMeshCharacter.MoveToDestination(hitResult.point);
}
}
}
This utilizes the NavMeshCharacter
to instruct the character to move to the world position where the player clicked. Internally, this uses a NavMeshAgent
to intelligently traverse the world.
Last updated