From 8722a9920c1f6119bf6e769cba270e63097f8e25 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 23 May 2024 10:08:29 +0800 Subject: + astar project --- .../ExampleScenes/ExampleScripts/ObjectPlacer.cs | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/ObjectPlacer.cs (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/ObjectPlacer.cs') diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/ObjectPlacer.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/ObjectPlacer.cs new file mode 100644 index 0000000..bd266ea --- /dev/null +++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/ObjectPlacer.cs @@ -0,0 +1,88 @@ +using UnityEngine; + +namespace Pathfinding.Examples { + /// Small sample script for placing obstacles + [HelpURL("https://arongranberg.com/astar/documentation/stable/objectplacer.html")] + public class ObjectPlacer : MonoBehaviour { + /// + /// GameObject to place. + /// When using a Grid Graph you need to make sure the object's layer is included in the collision mask in the GridGraph settings. + /// + public GameObject go; + + /// Flush Graph Updates directly after placing. Slower, but updates are applied immidiately + public bool direct = false; + + /// Issue a graph update object after placement + public bool issueGUOs = true; + + /// Align created objects to the surface normal where it is created + public bool alignToSurface = false; + + /// Global offset of the placed object relative to the mouse cursor + public Vector3 offset; + + /// Randomize rotation of the placed object + public bool randomizeRotation = false; + + float lastPlacedTime; + + /// Update is called once per frame + void Update () { + // Check if P is being pressed. + // Don't place objects if ctrl is pressed to avoid conflicts with the pause shortcut (ctrl+shift+P) + if (!Input.GetKey(KeyCode.LeftControl) && (Input.GetKeyDown("p") || (Input.GetKey("p") && Time.time - lastPlacedTime > 0.3f))) { + PlaceObject(); + } + + if (Input.GetKeyDown("r")) { + RemoveObject(); + } + } + + public void PlaceObject () { + lastPlacedTime = Time.time; + Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); + + // Figure out where the ground is + if (Physics.Raycast(ray, out var hit, Mathf.Infinity, ~0)) { + Vector3 p = hit.point + offset; + var rot = Quaternion.identity; + if (alignToSurface) rot = Quaternion.LookRotation(hit.normal, Vector3.right) * Quaternion.Euler(90, 0, 0); + if (randomizeRotation) rot = Random.rotation; + GameObject obj = GameObject.Instantiate(go, p, rot) as GameObject; + + if (issueGUOs) { + Bounds b = obj.GetComponent().bounds; + GraphUpdateObject guo = new GraphUpdateObject(b); + AstarPath.active.UpdateGraphs(guo); + if (direct) { + AstarPath.active.FlushGraphUpdates(); + } + } + } + } + + public void RemoveObject () { + Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); + + // Check what object is under the mouse cursor + if (Physics.Raycast(ray, out var hit, Mathf.Infinity)) { + // Ignore ground and triggers + if (hit.collider.isTrigger || hit.transform.gameObject.name == "Ground") return; + + Bounds b = hit.collider.bounds; + Destroy(hit.collider); + Destroy(hit.collider.gameObject); + + if (issueGUOs) { + GraphUpdateObject guo = new GraphUpdateObject(b); + AstarPath.active.UpdateGraphs(guo); + if (direct) { + AstarPath.active.FlushGraphUpdates(); + } + } + } + } + } +} -- cgit v1.1-26-g67d0