Search Unity

A little help with object rotation in game

Discussion in 'Scripting' started by lib87, Sep 9, 2015.

  1. lib87

    lib87

    Joined:
    Aug 28, 2015
    Posts:
    17
    Hi everyone..

    I have a box in my game.. I want to rotate it with mouse click and drag.. I have a method for that (CSharp) and in a way its working but not perfectly... I hope someone can help me with it.


    Code (CSharp):
    1. float rotationY = Input.GetAxis ("Mouse Y") * sensitivityY;
    2. this.transform.Rotate(Vector3.right, Mathf.Deg2Rad * rotationY);
    This works normally. The problem is is rotates left right / up down but its not rotating the object smoothly. What i mean is i think if i turn the box in one axis it take that surface as its orbit. So later on if i click to a surface and drag my mouse from bottom to up it might rotate the object left to right instead of down to up.. If im not very clear please let me know i can record a video i think..
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You're having a gimbaling problem. I almost always recommend against using transform.Rotate for exactly this reason.

    Track your rotationY across frames (as well as your rotationX assuming you have one), and instead of using transform.Rotate, create a new Quaternion from scratch using your tracked values and one of Quaternion's many helper functions (probably .Euler), and set transform.rotation to that.
     
  3. lib87

    lib87

    Joined:
    Aug 28, 2015
    Posts:
    17
    Is there any tutorial for that i can follow ?

    Thanks for the reply btw
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
  5. lib87

    lib87

    Joined:
    Aug 28, 2015
    Posts:
    17
    I will check that directly and write the result in here thank you
     
  6. lib87

    lib87

    Joined:
    Aug 28, 2015
    Posts:
    17
    I used this code but i think its still same.

    Code (CSharp):
    1. Vector3 position = _mouseReference - Input.mousePosition;
    2. this.transform.localRotation *= Quaternion.Euler(-position.y*_sensitivity,position.x*_sensitivity,0);
    Btw i wanted to explain differently once again cuz maybe its not gimbal lock problem after all..

    When cube is looking to me i can put my mouse to bottom and drag upwards which cause a movement up. if i drag from left to right then that is fine too but after this if i drag and drop from bottom to up instead my box moves in the screen right to left bcz the forward surface is turned when we rotate from left to right... I need my box to move (if i drag from bottom to up) same all the time where ever it looks..
     
    Last edited: Sep 9, 2015
  7. lib87

    lib87

    Joined:
    Aug 28, 2015
    Posts:
    17
    Seems like nobody knows the answer
     
  8. lib87

    lib87

    Joined:
    Aug 28, 2015
    Posts:
    17
    This code almost solved my problem if anybody looking for an answer for this problem...

    gameObject.transform.Rotate(Vector3.right, Mathf.Deg2Rad * rotationY,Space.World);
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the Mathf.Deg2Rad isn't actually doing anything there that a simple scalar value would do perfectly fine (it's just a way to get at the value of (PI * 2) / 360 anyway). Given that rotations in unity are handled by the engine as quaternions regardless it's a little misleading. The key point is the Space.World setting.
     
    lib87 likes this.