I am making a VR game in zero gravity, and in it the player can hold the grip button to grab onto an object. If they grab the level, I want the controllers to essentially lock into place and the play area to rotate around the controller, like so:
However, if you rotate the controller greater than about 90 degrees either direction on any axis, this happens:
It only happens when the controller is rotated greater than about 90 degrees either direction on any axis relative to the play area they are parented to, it does not matter how the play area is rotated in the world. Here is my code so far:
using UnityEngine;
using System.Collections;
public class Controllers : MonoBehaviour {
private Valve.VR.EVRButtonId gripButton = Valve.VR.EVRButtonId.k_EButton_Grip;
public bool gripButtonDown = false;
public bool gripButtonUp = false;
public bool gripButtonPressed = false;
private Valve.VR.EVRButtonId trigButton = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger;
public bool trigButtonDown = false;
public bool trigButtonUp = false;
public bool trigButtonPressed = false;
private Valve.VR.EVRButtonId appButton = Valve.VR.EVRButtonId.k_EButton_ApplicationMenu;
public bool appButtonDown = false;
public bool appButtonUp = false;
public bool appButtonPressed = false;
private SteamVR_TrackedObject trackedobj;
public SteamVR_PlayArea playarea;
private SteamVR_Controller.Device controller { get { return SteamVR_Controller.Input((int) trackedobj.index); } }
Vector3 grabpos = Vector3.zero;
Vector3 grabdisplace = Vector3.zero;
Vector3 grabareapos = Vector3.zero;
Vector3 grabareavel = Vector3.zero;
Quaternion grabrotation;
Quaternion grabrotsince;
// Use this for initialization
void Start () {
trackedobj = GetComponent <SteamVR_TrackedObject> ();
}
// Update is called once per frame
void Update () {
if(controller == null){
return;
}
gripButtonDown = controller.GetPressDown (gripButton);
gripButtonUp = controller.GetPressUp (gripButton);
gripButtonPressed = controller.GetPress (gripButton);
trigButtonDown = controller.GetPressDown (trigButton);
trigButtonUp = controller.GetPressUp (trigButton);
trigButtonPressed = controller.GetPress (trigButton);
appButtonDown = controller.GetPressDown (appButton);
appButtonUp = controller.GetPressUp (appButton);
appButtonPressed = controller.GetPress (appButton);
if (gripButtonDown) {
grab ();
}
if (gripButtonPressed) {
hold ();
}
if (gripButtonUp) {
drop ();
}
}
void grab (){
grabpos = transform.position;
grabrotation = transform.rotation;
}
public void hold (){
grabrotsince.w = playarea.transform.rotation.w + (grabrotation.w - transform.rotation.w);
grabrotsince.x = playarea.transform.rotation.x + (grabrotation.x - transform.rotation.x);
grabrotsince.y = playarea.transform.rotation.y + (grabrotation.y - transform.rotation.y);
grabrotsince.z = playarea.transform.rotation.z + (grabrotation.z - transform.rotation.z);
playarea.transform.rotation = grabrotsince;
grabdisplace = grabpos - transform.position;
playarea.transform.position = grabdisplace + playarea.transform.position;
grabareavel = playarea.transform.position - grabareapos;
grabareapos = playarea.transform.position;
}
void drop (){
playarea.GetComponent<Rigidbody> ().velocity = Vector3.zero;
playarea.GetComponent<Rigidbody> ().angularVelocity = Vector3.zero;
playarea.GetComponent<Rigidbody> ().velocity = ((grabareavel) / Time.deltaTime);
}
}
I assume this is being caused by my extremely basic understanding of how Quaternions work and as a result my incorrect manipulation of them. Any help figuring out how to fix this would be much appreciated. Also, if possible, I want the player to keep the rotational momentum they have while in grab mode when they release the grip button and exit grab mode. I have it figured out for translation, but rotation has me stumped. Any help is much appreciated! Thank you!

