VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_BasicGrabProvider.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.Events;
5 
6 using AVR.Core;
7 
8 namespace AVR.Phys {
9  /// <summary>
10  /// Simplest GrabProvider. Grabbed objects will move their center (obj.transform.position) towards the respective grabpoint.
11  /// </summary>
12  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_phys_1_1_a_v_r___basic_grab_provider.html")]
14  {
15  /// <summary>
16  /// Executed when an object is (successfully) grabbed.
17  /// </summary>
18  public UnityEvent OnGrab;
19 
20  /// <summary>
21  /// Executed when a grabbed object is released.
22  /// </summary>
23  public UnityEvent OnRelease;
24 
25  /// <summary>
26  /// Returns true if this provider is currently grabbing an object.
27  /// </summary>
28  public bool isGrabbing => grabbedObject != null;
29 
30  /// <summary>
31  /// Event that *commences* a grab. (Such as ONTRIGGERDOWN)
32  /// </summary>
34 
35  /// <summary>
36  /// Event that *ends* a grab. (Such as ONTRIGGERUP)
37  /// </summary>
39 
40  /// <summary>
41  /// Transform of the location an object is grabbed towards (Typically the palm of your hand).
42  /// Will be set automatically to the local transfrom if null.
43  /// </summary>
44  public Transform grabPoint;
45 
46  /// <summary>
47  /// Area from which the player may grab an object.
48  /// Required for the grabprovider to work.
49  /// </summary>
51 
52  /// <summary>
53  /// Object that is currently being grabbed. Null if no object is being grabbed
54  /// </summary>
55  protected AVR_Grabbable grabbedObject => grabLocation!=null ? grabLocation.grabbable : null;
56 
57  /// <summary>
58  /// Object that is currently being grabbed. Null if no object is being grabbed
59  /// </summary>
60  public AVR_Grabbable getGrabbedObject() => grabbedObject;
61 
63 
64  /// <summary>
65  /// Offset at which an object is being grabbed. Meaning: grab-position in local coordiantes relative to the
66  /// grabbed object. For instance: If we grab a Pan by its handle, this value will correspond to the pans handle
67  /// in local coordiantes.
68  /// If no object is grabbed, returns Vector3.zero.
69  /// </summary>
70  public virtual Vector3 getLocalGrabLocation() {
71  return grabbedObject ? grabLocation.localLocation : Vector3.zero;
72  }
73 
74  /// <summary>
75  /// Location at which the object is being grabbed in world coordinates.
76  /// Example: If we grab a pan by its handle, the handles world coordinates will correspond to this.
77  /// Returns Vector3.zero if no object is being grabbed.
78  /// </summary>
79  public virtual Vector3 getWorldGrabLocation() {
80  return grabbedObject ? grabLocation.location : Vector3.zero;
81  }
82 
83  protected override void Start() {
84  if(grabPoint==null) grabPoint = transform;
85 
86  if (grabbableFinder == null) grabbableFinder = GetComponentInChildren<AVR_GrabbableFinder>();
87  if (grabbableFinder == null) {
88  AVR_DevConsole.error("Grabprovider " + gameObject.name + " has no grabbableFinder assigned! Destroying " + gameObject.name);
89  Destroy(gameObject);
90  }
91  }
92 
93  /// <summary>
94  /// Target position the grabbed object should "strive" towards in world coordiantes.
95  /// There will be force pulling getWorldGrabPosition and getTargetPosition together.
96  /// </summary>
97  public virtual Vector3 getTargetPosition() {
98  return grabPoint.position;
99  }
100 
101  /// <summary>
102  /// Target rotation the grabbed object should "strive" towards in world space.
103  /// There will be force trying to make the objects rotation and this rotation equal.
104  /// </summary>
105  public virtual Quaternion getTargetRotation() {
106  return grabPoint.rotation;
107  }
108 
109  /// <summary>
110  /// getTargetPosition() and getTargetRotation() combined into one transform.
111  /// The grabbed objects transform will strive to adopt these values over time.
112  /// </summary>
113  public virtual Transform getTargetTransform() {
114  return grabPoint;
115  }
116 
117  /// <summary>
118  /// Performs a grab on whichever object the GrabbableFinder returns. Is called when the respective "grabEvent" is true.
119  /// </summary>
120  public virtual void makeGrab() {
121  // Get the collider that is closest to the grabPoint
122  if(grabbableFinder.getGrabLocation(out GrabLocation location)) makeGrab(location);
123  else grabLocation = null;
124  }
125 
126  /// <summary>
127  /// Perform a grab on with parameters given in a GrabLocation struct
128  /// </summary>
129  public virtual void makeGrab(GrabLocation location) {
130  OnGrab.Invoke();
131  grabLocation = location;
132  grabbedObject.Grab(this);
133  }
134 
135  /// <summary>
136  /// Ends a grab. Is called when the respective "releaseEvent" is true.
137  /// </summary>
138  public virtual void makeRelease() {
139  if(grabbedObject !=null) {
140  OnRelease.Invoke();
141  grabbedObject.Release(this);
142  grabLocation = null;
143  }
144  }
145 
146  protected virtual void Update() {
147 #if AVR_NET
148  if (IsOnline && !IsOwner) return;
149 #endif
150  // Make Grab
151  if(controller.inputManager.getEventStatus(grabEvent)) {
152  makeGrab();
153  }
154  // Make Release
155  else if(controller.inputManager.getEventStatus(releaseEvent)) {
156  makeRelease();
157  }
158  }
159  }
160 }
BoolEvent
Boolean-returning events of a controller.
AVR_ControllerInputManager.BoolEvent grabEvent
Event that commences a grab. (Such as ONTRIGGERDOWN)
AVR_GrabbableFinder grabbableFinder
Area from which the player may grab an object. Required for the grabprovider to work.
virtual Quaternion getTargetRotation()
Target rotation the grabbed object should "strive" towards in world space. There will be force trying...
virtual Vector3 getTargetPosition()
Target position the grabbed object should "strive" towards in world coordiantes. There will be force ...
virtual Vector3 getLocalGrabLocation()
Offset at which an object is being grabbed. Meaning: grab-position in local coordiantes relative to t...
abstract bool getGrabLocation(out GrabLocation location)
Transform grabPoint
Transform of the location an object is grabbed towards (Typically the palm of your hand)...
virtual Vector3 getWorldGrabLocation()
Location at which the object is being grabbed in world coordinates. Example: If we grab a pan by its ...
Represents an (attempted) grab at a given location of an object.
Represents a grabbable object.
virtual void makeGrab()
Performs a grab on whichever object the GrabbableFinder returns. Is called when the respective "grabE...
virtual Transform getTargetTransform()
getTargetPosition() and getTargetRotation() combined into one transform. The grabbed objects transfor...
AVR_ControllerInputManager.BoolEvent releaseEvent
Event that ends a grab. (Such as ONTRIGGERUP)
virtual void makeRelease()
Ends a grab. Is called when the respective "releaseEvent" is true.
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
Simplest GrabProvider. Grabbed objects will move their center (obj.transform.position) towards the re...
virtual void makeGrab(GrabLocation location)
Perform a grab on with parameters given in a GrabLocation struct
UnityEvent OnRelease
Executed when a grabbed object is released.
AVR_Component specifically attatched to an AVR_Controller. Needs to have an AVR_Controller on this ga...
UnityEvent OnGrab
Executed when an object is (successfully) grabbed.
Class to retrieve a Grabbable object from a location, volume or similar.