VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_AdvancedOffsetGrabProvider.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 namespace AVR.Phys {
6  /// <summary>
7  /// Equivalent to AVR_OffsetGrabProvider, but additionally allows for hand-to-object grab mode to be used.
8  /// </summary>
9  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_phys_1_1_a_v_r___advanced_offset_grab_provider.html")]
11  {
12  private Transform virtualGrabPoint;
13 
14  private Vector3 lastPos;
15  private float distance_travelled_since_grab = 0f;
16  private float normalization_distance = 0.5f;
17 
18  private Vector3 pos_offset;
19  private Quaternion rot_offset;
20 
21  public override Vector3 getTargetPosition() {
22  return virtualGrabPoint.position;
23  }
24 
25  public override Quaternion getTargetRotation() {
26  return virtualGrabPoint.rotation;
27  }
28 
29  public override Transform getTargetTransform() {
30  return virtualGrabPoint;
31  }
32 
33  protected override void Start()
34  {
35  base.Start();
36  virtualGrabPoint = new GameObject("virtualGrabPoint").transform;
37  virtualGrabPoint.SetParent(base.getTargetTransform());
38  virtualGrabPoint.localRotation = Quaternion.identity;
39  virtualGrabPoint.localPosition = Vector3.zero;
40  }
41 
42  public override void makeGrab(GrabLocation location)
43  {
44  base.makeGrab(location);
45  if (grabbedObject != null)
46  {
47  if(grabbedObject.objectType.handToObject) {
48  if(handVisual!=null) handVisual.glove_transform.position += grabLocation.location - grabPoint.position;
49 
50  virtualGrabPoint.position = grabbedObject.transform.position; //The target position is equal to the object position == dont move the object directly on-grab
51  virtualGrabPoint.rotation = grabbedObject.transform.rotation;
52  }
53 
54  // pos_offset is equal to the local position of virtualGrabPoint == difference to base.getTargetPosition()
55  pos_offset = virtualGrabPoint.localPosition;
56  rot_offset = virtualGrabPoint.localRotation;
57 
58  distance_travelled_since_grab = 0f;
59  }
60  }
61 
62  public override void makeRelease()
63  {
64  base.makeRelease();
65  if(handVisual) handVisual.glove_transform.localPosition = Vector3.zero; //cheaty
66  virtualGrabPoint.localPosition = Vector3.zero; //virtualgrabPoint is a child of base.getTargetTransform() so this is fine
67  virtualGrabPoint.localRotation = Quaternion.identity;
68  pos_offset = Vector3.zero;
69  rot_offset = Quaternion.identity;
70  }
71 
72  protected override void Update() {
73 #if AVR_NET
74  if (IsOnline && !IsOwner) return;
75 #endif
76  base.Update();
77 
78  if(grabbedObject!=null) {
79  float factor = distance_travelled_since_grab / normalization_distance;
80 
81  virtualGrabPoint.localPosition = Vector3.Lerp(pos_offset, Vector3.zero, factor);
82  virtualGrabPoint.localRotation = Quaternion.Lerp(rot_offset, Quaternion.identity, factor);
83 
84  distance_travelled_since_grab +=
85  Vector3.Distance(lastPos, virtualGrabPoint.position) +
86  0f
87  ;
88  }
89  else {
90  distance_travelled_since_grab = 0f;
91  }
92  lastPos = virtualGrabPoint.position;
93  }
94 
95  protected override IEnumerator setHand()
96  {
97  yield return StartCoroutine(base.setHand());
98  }
99 
100  protected override IEnumerator unsetHand()
101  {
102  yield return StartCoroutine(base.unsetHand());
103  }
104  }
105 }
override Quaternion getTargetRotation()
Target rotation the grabbed object should "strive" towards in world space. There will be force trying...
override Transform getTargetTransform()
getTargetPosition() and getTargetRotation() combined into one transform. The grabbed objects transfor...
Equivalent to AVR_OffsetGrabProvider, but additionally allows for hand-to-object grab mode to be used...
override void makeRelease()
Ends a grab. Is called when the respective "releaseEvent" is true.
Represents an (attempted) grab at a given location of an object.
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
override Vector3 getTargetPosition()
Target position the grabbed object should "strive" towards in world coordiantes. There will be force ...
override void makeGrab(GrabLocation location)
Perform a grab on with parameters given in a GrabLocation struct
Same as BasicGrabProvider but additionally allows grabbing objects at an offset. (Eg. you may grab a pan by its handle instead of just its center) Also allows the usage of an AVR_Hand. Use this if you require grabbing larger / more complex objects.