- Home /
Rotation that is dependent on button presses?
I have an object that moves using WASD along the ZX plane(the camera is overhead). When It moves in any direction, its rotation stays the same(it just moves along the plane), how can I make it so that when I press W it faces one direction, D is another, etc.? Thanks
Answer by Proclyon · Nov 08, 2010 at 11:38 AM
you have to change the current rotation of the object stored in the transform.
Changing the rotation of the Y axis of the object is probably what you want.
Y + 90 = Face 90 degrees to right. Y +180 = Face 180 degrees to right. etc.
You can also use negative values if you want , so Y - 90 is face left
Be carefull as this is an ABSOLUTE Value, if your camera rotates along side the camera or your character is not aligned to an original position and lives in a turning world. If one of these does apply you will need RELATIVE positions.
To do that get the Camera.main.transform and extract from the camera transform the current rotation or the Vector information. Use that as , like this example possibly*BELOW*, to get the Relative information to always face the correct side.
Quaternion camRotation = Camera.main.rotation;
Quaternion myRotation = camRotation + /*myFacingDirection*/;
transform.rotation = myRotation;
If you change the rotation to match that of the camera + an addition of whatever you want you should be able to tweak a proper relative direction without to much effort. Just remember that the rotation is a Quaternion and you may need to be carefull when editing those. Just copy all the values you don't want to change and set the Y rotation value to per direction in a variable
Quaternion relativeLeft = new Quaternion(camRotation.x, camRotation.y - 1, camRotation.z, camRotation.w);
for right just use + 1 and so forth.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082314im_/https://answers.unity.com/themes/thub/images/avi.jpg)