0
\$\begingroup\$

I'm trying to develop a script that detects if an object's angles are tilting too much.

I am new to Unity, and I am sure this is not the best way to test an object's angles, but I am trying to learn C# so I'd rather figure that out myself.

All I want to know is how to check whether a value is between -101 and 101.

I have tried this so many different ways, and even though it is working fine for positive integers, the if statement is not applying the moment that my z angle becomes negative.

Here is what I have tried so far:

(maxangle = 101, minangle = -101)

if(Player.transform.rotation.eulerAngles.z > Mathf.Sign(-101) && 
Player.transform.rotation.eulerAngles.z < maxangle),

if(Player.transform.rotation.eulerAngles.z > minangle && 
Player.transform.rotation.eulerAngles.z < maxangle)
\$\endgroup\$
  • \$\begingroup\$ This is unfortunately a duplicate of a simpler form of your question: stackoverflow.com/questions/5023213/… but I think what you're after is basically an easy way to transform those angles into a standard thing, eg: Radians and perhaps a look for a good class to put it on so your Unity project can access it from anywhere. You'll learn that with practice. \$\endgroup\$ – Stephen J Nov 1 '18 at 19:02
  • \$\begingroup\$ This wasn't really what I was asking. I understand how to check in between variables but I want to know how to check in between a negative and positive integer. Or just how to check a negative integer. \$\endgroup\$ – loverman Nov 1 '18 at 19:10
  • \$\begingroup\$ It's the same thing. The answers for "two numbers" assumes they could go negative or positive \$\endgroup\$ – Stephen J Nov 1 '18 at 19:17
  • 1
    \$\begingroup\$ If you're asking how to check if a number is negative, that's also on an SO post as a separate thing. Basically, break your problem into solvable steps, then look up those steps \$\endgroup\$ – Stephen J Nov 1 '18 at 19:17
  • \$\begingroup\$ I only have one problem. The script is working perfectly between 0-100. My problem is that the moment a slips into a negative value the if statement returns false. I have no idea why it is doing this and it seems as if it doesnt know how to check negative integers. It sounds like I am looking for how to check a negative value but I wasn't able to find any posts about that. \$\endgroup\$ – loverman Nov 1 '18 at 20:41
1
\$\begingroup\$

Doing math on Euler angles can get you into trouble.

Under the hood, Unity uses quaternions to have simpler & more uniform rotational behaviour. It converts to Euler angles when it needs to display them in the inspector, but this conversion isn't unique. For instance, a z rotation of -5° is equivalent to a z rotation of +355°, and the engine might decide to give you the latter.

You can work around some common angular wraparound issues with the DeltaAngle method:

// This will wrap angles close to 360 around
// to the corresponding negatives instead
float difference = Mathf.DeltaAngle(0, transform.eulerAngles.z);

if(difference > minAngle && difference < maxAngle) {
    ...
}

This will behave well for rotations within a particular range, but the mapping from orientations to Euler angles has discontinuities, so you can get surprises if your object can take on any arbitrary orientation.

If what you want is to evaluate how much your object is leaning, comparing dot products of its local up or forward vectors can give you a more continuous and consistent measure, without the surprises of the arbitrary Euler angle mapping.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.