人物控制(点击屏幕移动角色)
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
//公开NavMeshAgent组件设为变量(可变组件方式)
public NavMeshAgent agent;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//检查鼠标左键是否按下(0代表左键)
if (Input.GetMouseButtonDown(0)){
//设定射线变量,赋予以摄像机发射射线获取的鼠标当前位置坐标
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//设定光线投射碰撞变量
RaycastHit hit;
//检查光线投射是否有碰撞点(射线的起点ray,碰撞点)
if (Physics.Raycast(ray,out hit)){
//调用NavMeshAgent目标点功能
agent.SetDestination(hit.point);
}
}
}
}