- Home /
How do i rotate an object relative to itself?
I started making a racing drone sim in Unity and ive already established things like physiks using rigidbody (gravity, relative thrust, collision) and attached the camera to the drone. I thought i had the controlls working but when the camera was attached i noticed that the rotation happend relative to the world and not the drone (i used this when "w" was pressed: transform.Rotate(transform.forward); i dont know why that doesnt work because i was able to get relative thrust with: rb = rigidbody, rb.AddForce(transform.up*thrust);) i tried getting the current rotation of the object and setting that as the default so that the following rotations would be happening relative to that using vector3 and quaternion. but since im pretty new to unity i dont really know how to do that. Maby im just thinking too complicated.
So to summerize: id like the object to rotate for example to the right relative to itself but i dont know how.
thanks for any help!
Answer by LOSTSOUL86 · Dec 29, 2019 at 10:21 AM
if you assign "Rotation" value it is always set to the global coordinates. To rotate object certain value from its current orientation use Rotate method. For example:
rotationAngle = 5; //degrees from the current orientation
camera.transform.Rotate(Vector3.forward * rotationAngle);
This means whatever object orientation is at the moment -> rotate game object 5 degrees right from this orientation.
Answer by xibanya · Dec 29, 2019 at 06:43 AM
It sounds like you'd like to use localRotation (accessed via transform.localRotation.) I could probably use more details about your specific case, but your object is already rotated in the "forward" direction. Multiplying a quaternion is effectively like adding, so you could keep the transform's original local rotation stored in a variable like private Quaternion startRotation;
and keep a private float horizontal;
In this example, you'd also have a public float turnSpeed;
in the inspector.
on update do something like
horizontal += Input.GetAxisRaw("Horizontal");
Quaternion targetRotation = startRotation * Quaternion.AngleAxis(horizontal, Vector3.up);
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, turnSpeed * Time.deltaTime);
Answer by presidentgod123 · Dec 29, 2019 at 04:43 PM
thanks for the replys but i found a solution while looking at someone elses question here: http://answers.unity.com/answers/1160252/view.html But as a newbee i still learned a lot from your answers. thank you
Your answer
Follow this Question
Related Questions
Quaternions, relative clamped rotation, look rotations in spaceship movement 0 Answers
Rotate object around certain point using phyics 0 Answers
Parent rotation on a BoxCollider2d 0 Answers
Set a rotation relative to position between two objects. 0 Answers
really annoying rotation problem. 0 Answers