Basics

Checked with version: 2017.3

-

Difficulty: Beginner

Learn how to create AI pathfinding using the Unity NavMesh components! This video was produced by Brackeys.

Basics

Beginner Navigation

Download the Example Project here.

PlayerController

Code snippet

using UnityEngine;
using UnityEngine.AI;

public class PlayerController : MonoBehaviour {

    public Camera cam;

    public NavMeshAgent agent;
    
    // Update is called once per frame
    void Update () 
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                agent.SetDestination(hit.point);
            }
        }
    }
}

Related tutorials