• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Nanoux · Jul 24, 2016 at 08:41 AM · rotationvrspaceshooterquaternions

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

VR: Controller rotation delta along other axis 1 Answer

How to rotate object based on another rotation. 1 Answer

Android fix DrawCells while shooting Bullet 0 Answers

How can I instantiate a gameobject facing another gameobject 2D? 0 Answers

Object deforms with rotation 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges