- Home /
how to set the rotation of a game object around the world axes ? (can't use transform.rotate)
I am using a joystick where the stick gives value ranging from -45 to 45 in two directions( vertically and horizontally ). So using this stick I want to rotate my game object around the world axes. When the stick is moved vertically it should change the rotation along x axis and when moved horizontally should rotate it around the world y axis. I can't use transform.rotate in this case since I want to give it a specific rotation. For example when i move the stick to 45 ,game object should rotate by 45 degrees and if then i move my stick to 40 ,it should set the game object at 40 degrees and likewise. I tried the following code but it doesn't seem to work.
Vector3 worldRotation = new Vector3(desired_x,transform.TransformDirection(transform.eulerAngles).y,desired_z);
transform.eulerAngles = transform.InverseTransformDirection(worldRotation);
I have been trying this since a very long time. can somebody help me with this? would really appreciate that
Answer by AjayKhara · Oct 09, 2014 at 11:11 AM
Why not try something like this,
transform.eulerAngles = new Vector3 (/*Joystick Vertical Value */, /*Joystick Horizontal Value */, 0);