@@ -25,6 +25,10 @@ private void Awake() {
2525 GameManager . Instance . OnEditorStateChanged += OnEditorStateChanged ;
2626 }
2727
28+ private void Start ( ) {
29+ InvokeRepeating ( "UpdateSight" , 0.1f , 0.1f ) ;
30+ }
31+
2832 private void OnEditorStateChanged ( object sender , EditorStateEventArgs args ) {
2933 switch ( args . Data ) {
3034 case GameManager . EditorStateEnum . Normal :
@@ -36,22 +40,10 @@ private void OnEditorStateChanged(object sender, EditorStateEventArgs args) {
3640 break ;
3741 }
3842 }
39- private void Update ( ) {
43+
44+ private void UpdateSight ( ) {
4045 Ray ray = Camera . main . ScreenPointToRay ( new Vector3 ( Screen . width / 2 , Screen . height / 2 , 0f ) ) ;
41- if ( SelectedGizmoAxis ? . GetInvocationList ( ) . Length > 0 ) {
42- RaycastHit [ ] hits = Physics . RaycastAll ( ray . origin , ray . direction ) ;
43- foreach ( RaycastHit hit in hits ) {
44- if ( hit . collider . gameObject . CompareTag ( "gizmo_x" ) ) {
45- SelectedGizmoAxis . Invoke ( this , new GizmoAxisEventArgs ( Gizmo . Axis . X ) ) ;
46- } else if ( hit . collider . gameObject . CompareTag ( "gizmo_y" ) ) {
47- SelectedGizmoAxis . Invoke ( this , new GizmoAxisEventArgs ( Gizmo . Axis . Y ) ) ;
48- } else if ( hit . collider . gameObject . CompareTag ( "gizmo_z" ) ) {
49- SelectedGizmoAxis . Invoke ( this , new GizmoAxisEventArgs ( Gizmo . Axis . Z ) ) ;
50- }
51- }
52- }
5346 if ( SelectorMenu . Instance . CanvasGroup . alpha > 0 && SelectorMenu . Instance . gameObject . activeSelf ) {
54-
5547
5648 RaycastHit hitinfo = new RaycastHit ( ) ;
5749 bool anyHit = false , directHit = false ;
@@ -122,8 +114,8 @@ private void Update() {
122114 }
123115 if ( h ) {
124116 items . Sort ( ( x , y ) => x . Item1 . CompareTo ( y . Item1 ) ) ;
125- /* if (items.Count > 10)
126- items.RemoveRange(10, items.Count - 10);*/
117+ /* if (items.Count > 10)
118+ items.RemoveRange(10, items.Count - 10);*/
127119 SelectorMenu . Instance . UpdateAimMenu ( items ) ;
128120 } else {
129121 SelectorMenu . Instance . UpdateAimMenu ( new List < Tuple < float , InteractiveObject > > ( ) ) ;
@@ -133,221 +125,24 @@ private void Update() {
133125 }
134126
135127 }
128+ }
136129
137- //if (SelectorMenu.Instance.Active) {
138- /* if (SelectorMenu.Instance.CanvasGroup.alpha > 0 && SelectorMenu.Instance.gameObject.activeSelf) {
139-
140- //if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)), out hit, Mathf.Infinity)) {
141- Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0f));
142-
143- RaycastHit hitinfo = new RaycastHit();
144- bool anyHit = false, directHit = false;
145- if (Physics.Raycast(ray, out RaycastHit hit)) {
146- hitinfo = hit;
147- anyHit = true;
148- directHit = true;
149- //Debug.DrawRay(ray.origin, ray.direction);
150- } else {
151- RaycastHit[] hits = Physics.BoxCastAll(ray.origin, new Vector3(0.03f, 0.03f, 0.0001f), ray.direction, Camera.main.transform.rotation);
152- if (hits.Length > 0) {
153-
154- float minDist = float.MaxValue;
155- foreach (RaycastHit h in hits) {
156- Vector3 dir = ray.direction;
157-
158- Vector3 point = ray.origin + dir * Vector3.Distance(ray.origin, h.point);
159- float dist = Vector3.Distance(point, h.collider.ClosestPointOnBounds(point));
160- Debug.DrawLine(point, h.point);
161- Debug.DrawRay(ray.origin, ray.direction);
162- if (dist < minDist) {
163- hitinfo = h;
164- anyHit = true;
165- minDist = dist;
166- }
167- }
168- }
169- }
170- if (anyHit) {
171- Vector3 lhs = hitinfo.point - ray.origin;
172-
173- float dotP = Vector3.Dot(lhs, ray.direction.normalized);
174- Vector3 point = ray.origin + ray.direction.normalized * dotP;
175- List<Tuple<float, InteractiveObject>> items = new List<Tuple<float, InteractiveObject>>();
176- bool h = false;
177- foreach (SelectorItem item in SelectorMenu.Instance.SelectorItems.Values) {
178- if (!item.InteractiveObject.Enabled)
179- continue;
180- try {
181- if (item.InteractiveObject == null) {
182- continue;
183-
184- }
185- float dist = item.InteractiveObject.GetDistance(hitinfo.point);
186-
187- foreach (Collider c in item.InteractiveObject.Colliders) {
188-
189- if (c == hitinfo.collider) {
190- dist = 0;
191- h = true;
192- }
193-
194- }
195-
196-
197- if (item.InteractiveObject is ActionObjectNoPose || dist > 0.2) { // add objects max 20cm away from point of impact
198-
199- Debug.DrawLine(ray.origin, hitinfo.point);
200- continue;
201- }
202-
203- items.Add(new Tuple<float, InteractiveObject>(dist, item.InteractiveObject));
204- } catch (MissingReferenceException ex) {
205- Debug.LogError(ex);
206- Debug.LogError($"{item.InteractiveObject.GetName()}: {hitinfo.collider.name}");
207- }
208- }
209- if (h) {
210- items.Sort((x, y) => x.Item1.CompareTo(y.Item1));
211- SelectorMenu.Instance.UpdateAimMenu(items);
212- } else {
213- SelectorMenu.Instance.UpdateAimMenu(new List<Tuple<float, InteractiveObject>>());
214- }
215- } else {
216- SelectorMenu.Instance.UpdateAimMenu(new List<Tuple<float, InteractiveObject>>());
217- }
218-
219- }
220- */
221-
222- /*if (SelectorMenu.Instance.CanvasGroup.alpha == 0 || !SelectorMenu.Instance.gameObject.activeSelf)
223- return;
224- //if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)), out hit, Mathf.Infinity)) {
130+ private void FixedUpdate ( ) {
225131 Ray ray = Camera . main . ScreenPointToRay ( new Vector3 ( Screen . width / 2 , Screen . height / 2 , 0f ) ) ;
226-
227- RaycastHit hitinfo = new RaycastHit();
228- bool anyHit = false;
229- if (Physics.Raycast(ray, out RaycastHit hit)) {
230- hitinfo = hit;
231- anyHit = true;
232- } else {
233- RaycastHit[] hits = Physics.BoxCastAll(ray.origin, new Vector3(0.03f, 0.03f, 0.0001f), ray.direction, Camera.main.transform.rotation);
234- if (hits.Length > 0) {
235- float minDist = float.MaxValue;
236- foreach (RaycastHit h in hits) {
237- Vector3 dir = ray.direction;
238-
239- Vector3 point = ray.origin + dir * Vector3.Distance(ray.origin, h.point);
240- float dist = Vector3.Distance(point, h.collider.ClosestPointOnBounds(point));
241- Debug.DrawLine(point, h.point);
242- Debug.DrawRay(ray.origin, ray.direction);
243- if (dist < minDist) {
244- hitinfo = h;
245- anyHit = true;
246- minDist = dist;
247- }
248- }
249- }
250- }
251- if (anyHit) {
252- Vector3 lhs = hitinfo.point - ray.origin;
253-
254- float dotP = Vector3.Dot(lhs, ray.direction.normalized);
255- Vector3 point = ray.origin + ray.direction.normalized * dotP;
256- SelectorMenu.Instance.UpdateAimMenu(hitinfo.point);
257- } else {
258- SelectorMenu.Instance.UpdateAimMenu(null);
259- }*/
260-
261- /*
262- RaycastHit[] hits = Physics.BoxCastAll(ray.origin, new Vector3(0.03f, 0.03f, 0.0001f), ray.direction, Camera.main.transform.rotation);
263- if (hits.Length > 0) {
264- float minDist = float.MaxValue;
265- foreach (RaycastHit h in hits) {
266- Vector3 dir = ray.direction;
267- dir.Normalize();
268- Vector3 point = ray.origin + dir * Vector3.Distance(ray.origin, h.point);
269- float dist = Vector3.Distance(point, h.collider.ClosestPointOnBounds(point));
270- Debug.DrawLine(point, h.point);
271- Debug.DrawRay(ray.origin, ray.direction);
272- if (dist < minDist) {
273- hitinfo = h;
274- minDist = dist;
132+ if ( SelectedGizmoAxis ? . GetInvocationList ( ) . Length > 0 ) {
133+ RaycastHit [ ] hits = Physics . RaycastAll ( ray . origin , ray . direction ) ;
134+ foreach ( RaycastHit hit in hits ) {
135+ if ( hit . collider . gameObject . CompareTag ( "gizmo_x" ) ) {
136+ SelectedGizmoAxis . Invoke ( this , new GizmoAxisEventArgs ( Gizmo . Axis . X ) ) ;
137+ } else if ( hit . collider . gameObject . CompareTag ( "gizmo_y" ) ) {
138+ SelectedGizmoAxis . Invoke ( this , new GizmoAxisEventArgs ( Gizmo . Axis . Y ) ) ;
139+ } else if ( hit . collider . gameObject . CompareTag ( "gizmo_z" ) ) {
140+ SelectedGizmoAxis . Invoke ( this , new GizmoAxisEventArgs ( Gizmo . Axis . Z ) ) ;
275141 }
276142 }
277- Vector3 lhs = hitinfo.point - ray.origin;
278-
279- float dotP = Vector3.Dot(lhs, ray.direction.normalized);
280- Vector3 point2 = ray.origin + ray.direction.normalized * dotP;
281- SelectorMenu.Instance.UpdateAimMenu(point2);
282- } else {
283- SelectorMenu.Instance.UpdateAimMenu(null);
284143 }
285- */
286- /*ExtDebug.DrawBoxCastBox(ray.origin, new Vector3(0.05f, 0.05f, 0.00001f), Camera.main.transform.rotation, ray.direction, 20f, Color.green);
287- List<Tuple<float, InteractiveObject>> orderedTransforms = new List<Tuple<float, InteractiveObject>>();
288- if (hits.Length > 0) {
289- RaycastHit hit = hits.First();
290- GameManager.Instance.GetAllInteractiveObjects();
291- }*/
292- /*foreach (RaycastHit hit in hits) {
293-
294- float dist = Vector3.Cross(ray.direction, hit.point - ray.origin).magnitude;
295- InteractiveObject interactiveObject = hit.collider.transform.gameObject.GetComponent<InteractiveObject>();
296- if (interactiveObject is null) {
297- OnClickCollider collider = hit.collider.transform.gameObject.GetComponent<OnClickCollider>();
298- if (collider is null) {
299- continue;
300- }
301- interactiveObject = collider.Target.gameObject.GetComponent<InteractiveObject>();
302- if (interactiveObject is null) {
303- continue;
304- }
305- }
306- if (!InteractiveObjectInList(orderedTransforms, interactiveObject)) {
307- orderedTransforms.Add(new Tuple<float, InteractiveObject>(dist, interactiveObject));
308- }
309-
310- //hit.collider.transform.gameObject.SendMessage("OnHoverStart");
311- /*try {
312- if (CurrentObject == null) {
313- hit.collider.transform.gameObject.SendMessage("OnHoverStart");
314- HoverStartTime = System.DateTime.UtcNow;
315- CurrentObject = hit.collider.transform.gameObject;
316- } else {
317- if (!GameObject.ReferenceEquals(hit.collider.transform.gameObject, CurrentObject)) {
318- CurrentObject.SendMessage("OnHoverEnd");
319- if (endingHover) {
320- StopAllCoroutines();
321- endingHover = false;
322- }
323- hit.collider.transform.gameObject.SendMessage("OnHoverStart");
324- HoverStartTime = System.DateTime.UtcNow;
325- CurrentObject = hit.collider.transform.gameObject;
326- } else {
327-
328- if (endingHover) {
329- StopAllCoroutines();
330- endingHover = false;
331- HoverStartTime = System.DateTime.UtcNow;
332- }
333- }
334- }
335- } catch (Exception e) {
336- Debug.LogError(e);
337- }*/
338- /*}
339-
340- orderedTransforms.Sort((x, y) => x.Item1.CompareTo(y.Item1));
144+
341145
342- SelectorMenu.Instance.UpdateAimMenu(orderedTransforms);
343- //SelectorMenu.Instance.UpdateAimMenu(orderedTransforms.Select(_ => _.Item2).Distinct().ToList());
344-
345- /*else {
346- if (CurrentObject != null) {
347- if (!endingHover)
348- StartCoroutine(HoverEnd());
349- }
350- }*/
351146 }
352147
353148 private bool InteractiveObjectInList ( List < Tuple < float , InteractiveObject > > list , InteractiveObject interactiveObject ) {
0 commit comments