Easy Character Movement 2
  • Easy Character Movement 2
  • User Manual
    • General
      • Package Contents
      • Getting Started
      • Change Log
      • Components
        • Character
        • NavMesh Character
        • Character Movement
        • Slope Limit Behaviour
        • Physics Volume
        • Root Motion Controller
      • Examples
  • WALKTHROUGH
    • Creating a Character
    • Controlling a Character
    • Animating a Character
    • Extending a Character
    • Collision Detection and Events
    • Physics
    • Moving Platforms
    • AI Navigation
    • Cinemachine
  • SUPPORT
    • Customer Support
Powered by GitBook
On this page
  1. WALKTHROUGH

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.

PreviousMoving PlatformsNextCinemachine

Last updated 1 year ago