- 最后登录
- 2021-7-6
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 76145
 
- 纳金币
- 53488
- 精华
- 316
|
- 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;
- }
- }
- }
复制代码 |
|