纳金网
标题:
用NavMesh实现点击地面一点使角色避开障碍到达点击点
[打印本页]
作者:
烟雨
时间:
2015-8-30 00:47
标题:
用NavMesh实现点击地面一点使角色避开障碍到达点击点
using UnityEngine;
using System.Collections;
public class TTT : MonoBehaviour {
private NavMeshAgent agent;
public float minDistance = 3; //控制停下点与点击点间的距离
// Use this for initialization
void Start () {
agent = this.GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Physics.Raycast(ray, out hit);
Vector3 oriPos = transform.position;
Vector3 direction = hit.point - oriPos;
float length = (hit.point - oriPos).magnitude;
RaycastHit hitinfo;
bool isCollider = Physics.Raycast(oriPos, direction, out hitinfo, length);
if (isCollider)
{
print("ddd");
}
SetDestination(hit.point);
}
if (agent.enabled)
{
if (agent.remainingDistance < minDistance && agent.remainingDistance != 0)
{
agent.Stop();
agent.enabled = false;
}
}
}
void SetDestination(Vector3 targetPos)
{
agent.enabled = true;
agent.SetDestination(targetPos);
}
void StopAuto()
{
if (agent.enabled)
{
agent.Stop();
agent.enabled = false;
}
}
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5