This site uses strictly necessary cookies. More Information

X- Home /

# How to make the player move multiple directions on specific rotations?

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

**Answer** by robertbu
·
Jun 09, 2013 at 02:34 AM

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);
}
}
```

Oh I thought that it was supposed the even it out when two different rotations were being set. Thanks

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Sliding cube around on x/z axis with a constant y-axis 0 Answers

Lock ALL z-axis of project 2 Answers

Problems with the axes of the object 1 Answer

Z axis change 2 Answers

Move object relative to its axis 2 Answers