Search Unity

Rotate An Object Around Z Axis Relative To Camera

Discussion in 'Scripting' started by Sehlor, Aug 19, 2018.

  1. Sehlor

    Sehlor

    Joined:
    Feb 10, 2012
    Posts:
    199
    Hello everyone,

    I am developing a cube game but i am stuck at one point, the problem is that finger gesture to rotate the cube around its Z axis ( at default ). But i am also rotating cube around x and y so player see all of its faces. Problem begins here.

    Lets say if i rotate the cube on its back face facing the camera, Z axis is not the axis i want to Rotate gesture takes place anymore, because relative to Camera axis Z is rotating cube around X and Z.

    Let me explain more with graphical help:

    When X and Y rotations are 0,0 desired behaviour is achieved here: https://gyazo.com/38bffb715207cfd532dbc5f6ccd8126b

    But if i rotate the cube to see its other sides, and try to rotate it like before, behaviour changes ( naturally ) to this: https://gyazo.com/06fc28bad9501809efe8063afc0a9b48

    My question is, how can i calculate the rotation so it always compute the rotation according to face towards camera.

    Thank you.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    try this.
    Code (CSharp):
    1.  
    2. // put this outside method
    3. Vector3 targetAngle = new Vector3 () ;
    4. ...
    5. // this inside method
    6. targetAngle += deltaXYZ ;
    7. myTransform.rotation = Quaternion.Euler ( targetAngle ) * myTransform.rotation ; // global rotation
    I think something like that.

    Or you can put block in parent object. And rotate parent object instead, as reference frame.
     
  3. Sehlor

    Sehlor

    Joined:
    Feb 10, 2012
    Posts:
    199
    Thank you, this is the solution of my problem!