VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_GrabbableVolumeFinder.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 namespace AVR.Phys {
6  /// <summary>
7  /// Retrieves an AVR_Grabbable from a given volume (convex collider). The object closest to the given GrabPoint takes priority.
8  /// </summary>
9  [RequireComponent(typeof(Collider))]
10  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_phys_1_1_a_v_r___grabbable_volume_finder.html")]
12  {
13  public Transform GrabPoint;
14  List<Collider> colliders = new List<Collider>();
15 
16  private Collider col;
17 
18  protected override void Start()
19  {
20  if (!col) col = GetComponent<Collider>();
21  if (!GrabPoint) GrabPoint = GetComponentInParent<AVR_BasicGrabProvider>().grabPoint;
22  }
23 
24  public override Vector3 closestPoint(Vector3 pos)
25  {
26  return col.ClosestPoint(pos);
27  }
28 
29  private void OnTriggerEnter(Collider other)
30  {
31 #if AVR_NET
32  if (IsOnline && !IsOwner) return;
33 #endif
34  if (other.GetType() == typeof(MeshCollider) && !((MeshCollider)other).convex) return; //Dont add non-convex colliders
35  if (!colliders.Contains(other)) colliders.Add(other);
36  }
37 
38  private void OnTriggerExit(Collider other)
39  {
40 #if AVR_NET
41  if (IsOnline && !IsOwner) return;
42 #endif
43  if (colliders.Contains(other)) colliders.Remove(other);
44  }
45 
46  public override bool getGrabLocation(out GrabLocation location) {
47  float smallest_dist = float.PositiveInfinity;
48  Collider smallest_coll = null;
49  Vector3 closest_point = Vector3.zero;
50 
51  for (int i = 0; i < colliders.Count; i++)
52  {
53  if (!colliders[i])
54  {
55  colliders.RemoveAt(i);
56  i--;
57  continue;
58  }
59 
60  Collider c = colliders[i];
61  Vector3 p = c.ClosestPoint(GrabPoint.position);
62  if (Vector3.Distance(p, GrabPoint.position) < smallest_dist)
63  {
64  smallest_dist = Vector3.Distance(p, GrabPoint.position);
65  smallest_coll = c;
66  closest_point = p;
67  }
68  }
69 
70  if(smallest_coll==null) {
71  location = null;
72  return false;
73  }
74 
75  AVR_Grabbable grb = smallest_coll.GetComponentInParent<AVR_Grabbable>();
76 
77  if(grb==null) {
78  AVR.Core.AVR_DevConsole.cwarn("Attempted to grab a collider without an AVR_Grabbable component. Either 1) attatch an AVR_Grabbable to the respective object, or 2), disable collisions between the volumefinder and the given object.", this);
79  location = null;
80  return false;
81  }
82 
83  if(grb.grabNodes!=null) {
84  foreach(var node in grb.grabNodes) {
85  if(Vector3.Distance(GrabPoint.position, node.transform.position) < node.override_radius) {
86  location = new GrabLocation(grb, closest_point, smallest_dist, node);
87  return true;
88  }
89  }
90  }
91 
92  location = new GrabLocation(grb, closest_point, smallest_dist, smallest_coll);
93  return true;
94  }
95  }
96 }
Retrieves an AVR_Grabbable from a given volume (convex collider). The object closest to the given Gra...
List< AVR_GrabNode > grabNodes
List of GrabNodes that are attatched to this object
Represents an (attempted) grab at a given location of an object.
Represents a grabbable object.
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
override Vector3 closestPoint(Vector3 pos)
override bool getGrabLocation(out GrabLocation location)
Class to retrieve a Grabbable object from a location, volume or similar.