Unity之 - 导航网格寻路

地板、箱子、斜坡等行走路面设置,设置完点Bake。

Unity之 - 导航网格寻路_第1张图片


挂上NavMeshAgent 和 脚本

Unity之 - 导航网格寻路_第2张图片

SmartPlayer  物体行至鼠标所点位置

using UnityEngine;
using System.Collections;

public class SmartPlayer : MonoBehaviour
{
    NavMeshAgent agent;

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

    void Update()
    {
        RaycastHit hit;
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
                agent.SetDestination(hit.point);
        }
    }
}


Unity之 - 导航网格寻路_第3张图片

Follower 物体行至target所在位置

using UnityEngine;
using System.Collections;

public class Follower : MonoBehaviour
{
    public Transform target;
    private NavMeshAgent agent;

    // Use this for initialization
    void Start()
    {
        agent = GetComponent();
    }

    // Update is called once per frame
    void Update()
    {
        if (target != null)
            agent.destination = target.position;
    }
}



你可能感兴趣的:(Unity3D)