- Home /
Rotation in 8 way direction
Hello there, and first of all i know that there are ALOT of questions about rotation already answered, but i think that none of them seems to answer my problem ( i tried alot of things that came out of those ).
The thing is quite simple, using the arrows or WASD keys i want an object to rotate in the Y axis towards the same direction while having a static camera ( so i dont need a rotation based on the camera ). this is in a 3D way like the old games , for example : i press "up" and the object looks at 0º, i press "right" and the object looks at 90º, i press "up" and "right" and the object looks at 45º ( here is my problem ).
So far i have this code working but it seems pretty poor because it only works when i press "up " and "right" at the EXACT SAME time, otherwise if i press "up" and THEN press "right", it will still be looking at 0º.
if (Input.GetButtonDown("Up"))
{
transform.rotation=Quaternion.Euler(0,0,0);
}
if (Input.GetButtonDown("Right"))
{
transform.rotation=Quaternion.Euler(0,90,0);
}
// and here is what i can't figure out how to do it correctly
// tho i know that all this could be made in a different way
if (Input.GetButtonDown("Up") && Input.GetButtonDown("Right))
{
transform.rotation=Quaternion.Euler(0,45,0);
}
Thanks for any kind answer and sorry if someone asked something similar ( thought i looked around 20 other questions before doing this one to make sure no one asked this already)
PS: as you can see, i just want the object to change abrutly its rotation as the old good games, so there is no need to "smooth" the thingy. And i have already configured the input keys.
Is there a reason why you are not using the Input$$anonymous$$anager axis functionality? You could simply extract an orientation if you were using two axes of Input using left/right and up/down; we're using it for a similar use-case. Something like
transform.rotation=Quaternion.Euler(0,arctan(Input.GetAxis ("LookHorizontal")/Input.GetAxis ("LookVertical")),0);
That would cover all cases with a single function.
i used this for nothing in particular, i thought it would be easier this way ( dividing the axis functionality in 4 ), it was just the first thing that came to my $$anonymous$$d. Btw can you explain a little more the functionality of
arctan(Input.GetAxis ("LookHorizontal")/Input.GetAxis ("LookVertical")) ? thanks for your reply
Answer by save · Dec 28, 2012 at 10:28 PM
You could assign a new forward direction based on the horizontal and vertical axes.
function Update () {
if (Input.GetButton("Horizontal")||Input.GetButton("Vertical")) {
var newForward : Vector3 = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
if (newForward!=Vector3.zero) transform.forward = newForward;
transform.Translate(Vector3.forward*Time.deltaTime);
}
}
wow it worked pretty good thanks! i didn't thought about using forward direction
sorry to ask one more thing, i noticed that when you go into the opposite direction, the console says "Look rotation viewing vector is zero", its not a problem but i wonder why
Ah yes sorry, that happens when trying to set the forward direction to zero. Use the updated code sample to fix that issue.
Answer by Golan2781 · Dec 28, 2012 at 10:53 PM
What you want to do could be easier done using Input axes. Go to Edit -> Project Settings -> Input and define two axes here, one for left/right and one for up/down. Once you got that, the two axes can be seen as a direction vector. The construct
Vector2((Input.GetAxis ("Horizontal"),(Input.GetAxis ("Vertical"))
would for example point up with you'd press up or point to the lower right if you'd press down and right.
Seeing how you're only interested in the angle of the direction, you can calculate that directly using trigonometric functions. Arctan is the correct one in your situation; unity calculates the arctan in radians, so you also need to convert it to degrees.
Mathf.Atan((Input.GetAxis ("Horizontal")/Input.GetAxis ("Vertical"))*Mathf.Rad2Deg
thank you for your explanation, i did have those inputs but i was very confused about how to use them , so i divided them in 4 :P
Your answer
Follow this Question
Related Questions
how to know if an axis has been full rotated? 1 Answer
How can i rotate the aircraft elevator around the yellow axis up and down ?? as shown in the pic 0 Answers
Lock the Camera's Y Rotation Axis? 0 Answers
Rotating an object on Z axis while moving 1 Answer
Moving an object based on the position of another object 2 Answers