- Home /

# a variable that when an object is tilted 0º = 0 and 90º = 1

sounds simple, i know the math to do that. divide the rotation by 90 and you get it. anyway there is one problem with this: if the object tilts, for example, 15º on the x axis it would work, but if it tilts 15º in the oposite direction on the same axis the rotation will be something like 375º, and dividing this by 90 will certainly give me a number over 1. i need a way for it to give me a number between 0 and 1 regardless on the direction it tilts. and i certainly dont know how to do that math. ill keep on trying to achieve it but certainly a hand would be much apreciated. thanks to all.

p.s. if i was not clear on my explanation, which i probably wasnt, feel free to coment me or edit the post if u think there is a better way to explain it. thanks a lot for all your help, and happy new year.

So what *do* you want to return for an angle of 180' or 270'?

that's sine isn't it?

Math.sin

assuming you want a smooth oscillation, sin90 is 1, sin0 is 0, the other answers provide a more linear output

you are right, i think i will use that. it is briliant and simple. happy 2014

**Answer** by ArkaneX
·
Jan 01, 2014 at 12:38 AM

I guess the easiest option is just checking if your rotation for a given axis is greater than 180 and in this case subtracting it from 360. C# sample for x axis:

```
float rotation = transform.eulerAngles.x;
if (rotation > 180f)
{
rotation = 360f - rotation;
}
float tilt = rotation / 90f;
```

This sample will return values over 1 for rotation greater than 90 and lower than 270, but you can tune the sample to take this into consideration. Or if such angles are impossible in your case, then you can leave the sample as it is.

thanks dude, i had thought of using if satatements, though not in this way. now it seems realy simple. thanks a lot again and happy 2014

**Answer** by HappyMoo
·
Jan 01, 2014 at 12:52 AM

```
float magicNumber = (180-((angle+180)%360))/90;
```

Now this gives -1 to 1 (for -90 to +90) in case you also want to know the direction. If you don't care about direction, slap a Mathf.Abs() around the formula.

### 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

Rotate object based on FPSCamera 1 Answer

How to Rotate Object around point only 90 Degree. 1 Answer

2D - Rotate object based on touch moved (diff between touches) 0 Answers

How can you make a 2D object move based on rotation, and rotate while moving? 1 Answer

Trying to rotate my character to the position of my touch on mobile 1 Answer