- Home /
Problem rotating an object around a point
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!
Answer by Nanoux · Sep 14, 2016 at 01:41 PM
For anyone who may find this post down the line having a similar problem, i just solved it. Here's how:
playarea.transform.rotation = grabobj.transform.rotation; playarea.transform.rotation = playarea.transform.rotation * Quaternion.Inverse (transform.localRotation);
There's a complicated reason the above code doesn't work, but what this does is set the playarea's rotation to what i wan the controller's rotation to be, and then subtract the controllers local rotation from the playarea's new rotation to get the final needed rotation of the playarea. Have fun in space!
Your answer

Follow this Question
Related Questions
How to rotate object based on another rotation. 1 Answer
VR: Controller rotation delta along other axis 2 Answers
Rotation Complications + rotation with parents and children? 1 Answer
Rotating 2D sprite to match surface. 0 Answers
How to write a script to disable position and rotation tracking for VR 0 Answers