summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/ObjectPlacer.cs
blob: bd266eaa61925486a9e70fe07d8747b77632bef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using UnityEngine;

namespace Pathfinding.Examples {
	/// <summary>Small sample script for placing obstacles</summary>
	[HelpURL("https://arongranberg.com/astar/documentation/stable/objectplacer.html")]
	public class ObjectPlacer : MonoBehaviour {
		/// <summary>
		/// 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.
		/// </summary>
		public GameObject go;

		/// <summary>Flush Graph Updates directly after placing. Slower, but updates are applied immidiately</summary>
		public bool direct = false;

		/// <summary>Issue a graph update object after placement</summary>
		public bool issueGUOs = true;

		/// <summary>Align created objects to the surface normal where it is created</summary>
		public bool alignToSurface = false;

		/// <summary>Global offset of the placed object relative to the mouse cursor</summary>
		public Vector3 offset;

		/// <summary>Randomize rotation of the placed object</summary>
		public bool randomizeRotation = false;

		float lastPlacedTime;

		/// <summary>Update is called once per frame</summary>
		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<Collider>().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();
					}
				}
			}
		}
	}
}