1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using UnityEngine;
- using System.Linq;
- namespace Pathfinding {
-
-
-
-
-
-
-
-
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_target_mover.php")]
- public class TargetMover : MonoBehaviour {
-
- public LayerMask mask;
- public Transform target;
- IAstarAI[] ais;
-
- public bool onlyOnDoubleClick;
- public bool use2D;
- Camera cam;
- public void Start () {
-
- cam = Camera.main;
-
-
- ais = FindObjectsOfType<MonoBehaviour>().OfType<IAstarAI>().ToArray();
- useGUILayout = false;
- }
- public void OnGUI () {
- if (onlyOnDoubleClick && cam != null && Event.current.type == EventType.MouseDown && Event.current.clickCount == 2) {
- UpdateTargetPosition();
- }
- }
-
- void Update () {
- if (!onlyOnDoubleClick && cam != null) {
- UpdateTargetPosition();
- }
- }
- public void UpdateTargetPosition () {
- Vector3 newPosition = Vector3.zero;
- bool positionFound = false;
- if (use2D) {
- newPosition = cam.ScreenToWorldPoint(Input.mousePosition);
- newPosition.z = 0;
- positionFound = true;
- } else {
-
- RaycastHit hit;
- if (Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, mask)) {
- newPosition = hit.point;
- positionFound = true;
- }
- }
- if (positionFound && newPosition != target.position) {
- target.position = newPosition;
- if (onlyOnDoubleClick) {
- for (int i = 0; i < ais.Length; i++) {
- if (ais[i] != null) ais[i].SearchPath();
- }
- }
- }
- }
- }
- }
|