From 81330a6b68d307937c262368a42a3fa4e9ad4207 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 23 May 2024 10:10:23 +0800 Subject: + NavMeshTest --- .../NavMeshTest/Assets/Scripts/NavigateToTarget.cs | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Other/NavMeshTest/Assets/Scripts/NavigateToTarget.cs (limited to 'Other/NavMeshTest/Assets/Scripts/NavigateToTarget.cs') diff --git a/Other/NavMeshTest/Assets/Scripts/NavigateToTarget.cs b/Other/NavMeshTest/Assets/Scripts/NavigateToTarget.cs new file mode 100644 index 0000000..a7ed374 --- /dev/null +++ b/Other/NavMeshTest/Assets/Scripts/NavigateToTarget.cs @@ -0,0 +1,53 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.AI; + +public class NavigateToTarget : MonoBehaviour +{ + public Transform flag; + + public NavMeshAgent agent; + + private Transform m_Target; + private Vector3 m_TargetPosition; + + private void Awake() + { + agent = GetComponent(); + + agent.autoTraverseOffMeshLink = false; + } + + private void Update() + { + if (Input.GetMouseButtonDown(0)) + { + Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); + RaycastHit info; + if (Physics.Raycast(ray, out info, Mathf.Infinity, Physics.DefaultRaycastLayers)) + { + if (info.collider.gameObject.layer != LayerMask.NameToLayer("Prop")) + { + m_Target = info.collider.gameObject.transform; + m_TargetPosition = m_Target.InverseTransformPoint(info.point); + } + } + } + + if(m_Target != null) + { + Vector3 pos = m_Target.TransformPoint(m_TargetPosition); + + agent.destination = pos; + flag.position = pos; + } + + if(agent.isOnOffMeshLink) + { + + } + + } + +} -- cgit v1.1-26-g67d0