Nav Meshes

Checked with version: 4.3

-

Difficulty: Beginner

Want to learn how to create paths for the characters in your games? Looking to make an "On Rails" game? Curious how to perfectly roast a turkey? Stop in an learn 2 of those 3 things. In this lesson we will be looking at navigation meshes and how to implement them in your games. Tutor: Mike Geig

Nav Meshes

Beginner Navigation

NavCharScript

Code snippet

using UnityEngine;
using System.Collections;

public class NavCharScript : MonoBehaviour {

    NavMeshAgent agent;

    void Start ()
    {
        agent = GetComponent();
    }

    void Update () 
    {
        if(Input.GetMouseButtonDown (0))
        {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;

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

FollowScript

Code snippet

using UnityEngine;
using System.Collections;

public class FollowScript : MonoBehaviour {
    
    NavMeshAgent agent;
    
    void Start ()
    {
        agent = GetComponent();
    }
    
    void Update () 
    {
        agent.SetDestination (hit.point);
    }

}

Related tutorials

Related documentation