Tanoda
ObjectMove.cs
Go to the documentation of this file.
1using System;
2using UnityEngine;
3
4namespace Waypoint
5{
6 public class ObjectMove : MonoBehaviour
7 {
8 #region FIELDS
9
10 [Header("Settings")] [SerializeField] [Range(1.0f, 100.0f)]
11 private float _speedMove = 10.0f;
12
13 [SerializeField] [Range(1.0f, 100.0f)] private float _speedRotate = 50.0f;
14
15 [SerializeField] private Vector3 _offset = new Vector3(0.0f, 0.5f, 0.0f);
16
17 [SerializeField] private float _rayCastDistance = 1.0f;
18
19 private readonly bool _stop = false;
20 private Vector3 _position;
21 private Quaternion _rotation;
22
23 public GameObject lookAt;
24
25 public event Action OnEndOfRoad;
26
27 #endregion
28
29 #region PROPERTIES
30
31 private Vector3 Position
32 {
33 get
34 {
35 _position = transform.position - _offset;
36 return _position;
37 }
38
39 set
40 {
41 _position = value;
42 transform.position = _position + _offset;
43 }
44 }
45
46 private Quaternion Rotation
47 {
48 get
49 {
50 _rotation = transform.rotation;
51 return _rotation;
52 }
53
54 set
55 {
56 _rotation = value;
57 transform.rotation = _rotation;
58 }
59 }
60
61 #endregion
62
63 #region UNITY_METHODS
64
65 private void OnTriggerExit(Collider other)
66 {
67 if (!other.CompareTag("Obstacle")) return;
68
69 var obstacle = other.GetComponent<Obstacle>();
70 obstacle.PathIsBusy = false;
71 }
72
73 private void OnTriggerEnter(Collider other)
74 {
75 if (!other.CompareTag("Obstacle")) return;
76
77 var obstacle = other.GetComponent<Obstacle>();
78 obstacle.PathIsBusy = true;
79 }
80
81 private void OnDrawGizmos()
82 {
83 Gizmos.color = Color.magenta;
84 Gizmos.DrawRay(transform.position, transform.forward * (_rayCastDistance + transform.localScale.z));
85 }
86
87 #endregion
88
89 #region PUBLIC_METHODS
90
91 public void Move(Vector3 currentPoint)
92 {
93 if (_stop) return;
94
95 var targetLookRotation = currentPoint - Position;
96
97 if (targetLookRotation != Vector3.zero)
98 Rotation = Quaternion.Lerp(
99 transform.rotation,
100 Quaternion.LookRotation(lookAt.transform.position - Position, Vector3.up),
101 _speedRotate * Time.deltaTime);
102
103 Position = Vector3.MoveTowards(
104 Position,
105 currentPoint,
106 _speedMove * Time.deltaTime);
107
108 if (Vector3.Distance(Position, currentPoint) <= 0.1f) OnEndOfRoad?.Invoke();
109 }
110
111 #endregion
112 }
113}
UnityEngine.Color Color
Definition: TestScript.cs:32
GameObject lookAt
Definition: ObjectMove.cs:23
void Move(Vector3 currentPoint)
Definition: ObjectMove.cs:91