I only want my player to move on these Y rotations: 0, 45, 90, 135, 180, -45, -90, -135
How can I make him move into these rotations when I press buttons. Example:
Press W and D == Move at y rotation: 135
Here’s a picture of what I mean:
I only want him to move at those rotations. Here is my code:
if(Input.GetKeyDown("a"))
myTransform.rotation = Quaternion.Euler(0, 0, 0);
if(Input.GetKeyDown("w"))
myTransform.rotation = Quaternion.Euler(0, 90, 0);
if(Input.GetKeyDown("d"))
myTransform.rotation = Quaternion.Euler(0, 180, 0);
if(Input.GetKeyDown("s"))
myTransform.rotation = Quaternion.Euler(0, 270, 0);
The problem is that when I press two buttons at the same time, it does not turn to the rotation that is in the middle between the two. What is wrong? Thanks
With coding, you must code what you want to happen. Quaternion.Euler() sets absolute angles. Nothing here will give you an angle between the two. In addition, GetKeyDown() only returns true for a single frame. So even if this code were to somehow do a middle angle, the likelihood that you would get both key presses in a single frame is small. Here is a bit of a rewrite of your code in the direction you may want:
function Update() {
var myTransform = transform;
if(Input.GetKey("a")) {
if (Input.GetKey("w"))
myTransform.rotation = Quaternion.Euler(0, 45, 0);
else if (Input.GetKey("s"))
myTransform.rotation = Quaternion.Euler(0, 315, 0);
else
myTransform.rotation = Quaternion.Euler(0, 0, 0);
}
else if(Input.GetKey("w")) {
if (Input.GetKey("d"))
myTransform.rotation = Quaternion.Euler(0, 135, 0);
else
myTransform.rotation = Quaternion.Euler(0, 90, 0);
}
else if(Input.GetKey("d")) {
if (Input.GetKey("s"))
myTransform.rotation = Quaternion.Euler(0, 225, 0);
else
myTransform.rotation = Quaternion.Euler(0, 180, 0);
}
else if(Input.GetKey("s")) {
myTransform.rotation = Quaternion.Euler(0, 270, 0);
}
}