- Home /

# getAxis to rotation

Hey,

I would like to convert an Axis value like x1 y1 to an angle value like 45 degrees. Atm i "solved" this with a long if else but there has to be a better way of doing it?

thanks!

```
if (horAxis > 0)
{
crossHair.rotation = Quaternion.Euler (0, 0,0);
}else if (horAxis < 0)
{
crossHair.rotation = Quaternion.Euler (0, 0,180);
}
```

```
```

if (verAxis >0)
{
crossHair.rotation = Quaternion.Euler (0, 0,90);
}else if (verAxis <0)
{
crossHair.rotation = Quaternion.Euler (0, 0,-90);
}

if (horAxis > 0 && verAxis > 0)
{
crossHair.rotation = Quaternion.Euler (0, 0,45);
}
if (horAxis < 0 && verAxis < 0)
{
crossHair.rotation = Quaternion.Euler (0, 0,-135);
}
if (horAxis > 0 && verAxis < 0)
{
crossHair.rotation = Quaternion.Euler (0, 0,-45);
}
if (horAxis < 0 && verAxis > 0)
{
crossHair.rotation = Quaternion.Euler (0, 0,135);
}

please format your code sample by indenting the entire block with four spaces (and each subsequent indent should also be four spaces!).

**Answer** by duck
·
Dec 15, 2009 at 02:40 PM

If it's just two values representing a 2d vector (as in your example, x=1, y=1), you can use the Atan2 function provided in the Mathf collection of functions like this:

```
crossHair.rotation = Mathf.Atan2( horAxis, verAxis ) * Mathf.Rad2Deg;
```

Notice that you need to multiply the result by a special constant called Rad2Deg, because the result of Atan2 is in radians, wheras rotation values in Unity are measured in degrees.

You may find that you need to swap the input around or invert the values to get the final desired rotation, eg: (-verAxis, horAxis) or something similar.

Thanks a lot! i got a console error about not being able to convert to a float, so i changed it to the following:

crossHair.rotation = Quaternion.Euler(0,0,(Mathf.Atan2(verAxis , horAxis) * Mathf.Rad2Deg)); }

thanks again!

**Answer** by mike
·
Dec 16, 2009 at 08:37 AM

If you're not interested in the angle value and just want a quaternion, you can also use Quaternion.LookRotation() or Quaternion.FromToRotation().

### 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 user guide.

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

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

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

For troubleshooting common problems with Unity 5.x Editor (including Win 10).

### Follow this Question

### Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Rotating on the DragRigidBody.js 0 Answers

Character rotation help needed. 1 Answer

Object won't rotate z axis on second click? 2 Answers

Locking rotation on local axis 2 Answers