创建一个Plane
创建一个
Capsule
放在地面上,模拟为人勾上地面的
Navgiation Static
打开
Navigation
窗口,修改一下Agent Radius
为0.2,点击Bake
,点击时会提示保存场景,保存为需要的名字,比如说Main
即可新建一个名字为
Human
的脚本编写代码如下
using UnityEngine;
using System.Collections;
public class Human : MonoBehaviour {
public NavMeshAgent agent;
// Update is called once per frame
void Update () {
//鼠标左键按下
if(Input.GetMouseButtonDown(0)){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit)){
agent.SetDestination(hit.point);
}
}
}
}
为Capsule添加Nav Mesh Agent
为Capsule
挂载Human
脚本
将
Nav Mesh Agent
拖入Agent
位置进行赋值运行可发现功能已实现
如果需要摄像机跟随,先调整好摄像机
Main Camera
的位置,比如像作者这样新建一个名字为
FollowTarget
的脚本编写代码如下
using UnityEngine;
using System.Collections;
public class FollowTarget : MonoBehaviour {
//跟随目标
public Transform target;
//偏移
public Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - target.position;
}
// Update is called once per frame
void Update () {
transform.position = offset + target.position;
}
}
为Camera
挂载FollowTarget
脚本
拖入
Capsule
在Target
位置进行赋值运行即可
网友评论