导航移动 1.地形设置静态-烘焙路线
2.导航的对象,添加NavMeshAgent组件
3.写代码,设置目标
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerMouseMove02 : MonoBehaviour {
public GameObject arow;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//GetComponent<组件>().
//velocity速度以每秒距离单位(与物理相同)表示,并以全局坐标系表示。
print(GetComponent<NavMeshAgent>().speed + ":speed");
print(GetComponent<NavMeshAgent>().velocity + ":velocity");
//magnitude 点和原点之间的距离(x, y, z)->(0, 0, 0).
GetComponent<Animator>().SetBool("isRun", GetComponent <NavMeshAgent >().velocity.magnitude>0);
GetComponent<Animator>().SetFloat("Blend", GetComponent<NavMeshAgent>().velocity.magnitude );
if (Input.GetMouseButton (0))
{
//发射射线
//静态属性-1.全局唯一的属性,你可以通过类名很方便的调用。
//如果是对象的属性,你就要先找到对象,再调用属性,很麻烦
//例GameObject.Find();
//ViewportPointToRay是比例,mousePosition是像素
//Ray 射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition );
//RaycastHit 结构用于从光线广播中获取信息。
RaycastHit hit;
//Physics.Raycast:投射光线,从点开始origin方向direction,长度maxDistance与现场的所有对撞机相撞。 您可以选择提供层面具,以过滤掉任何您不感兴趣的对撞机产生碰撞。
//out 是输出参数的关键字
if (Physics.Raycast(ray,out hit,100f))
{
//判断是否碰撞倒了地面 collider所有对撞机的基类。
if (hit.collider.tag=="Ground")
{
//transform.LookAt:旋转变换,使前向矢量指向/目标/的当前位置。
transform.LookAt(hit.point);
Instantiate(arow, hit.point, Quaternion.identity);
GetComponent<NavMeshAgent>().SetDestination(hit.point );
}
}
}
}
}