您的位置:首页 > 移动开发 > Unity3D

Unity之 - 导航网格寻路

2016-03-17 00:06 507 查看
地板、箱子、斜坡等行走路面设置,设置完点Bake。



挂上NavMeshAgent 和 脚本



SmartPlayer  物体行至鼠标所点位置

using UnityEngine;
using System.Collections;

public class SmartPlayer : MonoBehaviour
{
NavMeshAgent agent;

void Start()
{
agent = GetComponent<NavMeshAgent>();
}

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);
}
}
}




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<NavMeshAgent>();
}

// Update is called once per frame
void Update()
{
if (target != null)
agent.destination = target.position;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity