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 --- Other/NavMeshTest/Assets/Scripts/MoveToMouse.cs | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Other/NavMeshTest/Assets/Scripts/MoveToMouse.cs (limited to 'Other/NavMeshTest/Assets/Scripts/MoveToMouse.cs') diff --git a/Other/NavMeshTest/Assets/Scripts/MoveToMouse.cs b/Other/NavMeshTest/Assets/Scripts/MoveToMouse.cs new file mode 100644 index 0000000..81a2b1b --- /dev/null +++ b/Other/NavMeshTest/Assets/Scripts/MoveToMouse.cs @@ -0,0 +1,34 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.AI; + +public class MoveToMouse : MonoBehaviour +{ + public Transform flag; + + private NavMeshAgent m_Agent; + + private void Awake() + { + m_Agent = GetComponent(); + } + + 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_Agent.destination = info.point; + flag.position = info.point; + } + } + } + } + +} -- cgit v1.1-26-g67d0