Basics

Vérifié avec version: 2017.3

-

Difficulté: Débutant

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

Basics

Débutant 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);
            }
        }
    }
}

Tutoriels apparentés