- Home /
Detecting the range between 2 angles
Hey, trying to detect the range between 2 angles and then print a message for now.
At the moment, I'm using this; if( transform.rotation.eulerAngles.x >= 180 )
The problem with that is that, I don't want it to be greater than or less that 180 degrees, I want it detect when it's between 2 certain angles, like this.
I asked the question a few days ago but was unable to get an answer, I don't think I explained it properly. Also coding in c#
Answer by Destroyd · Feb 20, 2012 at 03:22 AM
You don't have to use that confusing Asin code. You can use Mathf.DeltaAngle(Angle1,Angle2)
Each angle can exceed 360 and it will still return 0-180 (the angle between them.)
http://unity3d.com/support/documentation/ScriptReference/Mathf.DeltaAngle.html
To actually detect the 2 angles, I think I might need an array, because the rotation on a cube goes from 0 - 90, then back from 90 - 0 if you rotate in a full 360 degree angle.
This is really confusing, lol.
I'm confused as to what you're trying to do. Do you have two angles (a $$anonymous$$ and max) and you want to see if a specific angle lies between them? If that's the case then this is all you'd have to do: if (Angle>$$anonymous$$ && Angle
What I'm actually trying to do is make the "dragon" change into a "diving" state when it's between the two angles, heres my current code. using UnityEngine; using System.Collections;
public class DiveState : $$anonymous$$onoBehaviour { public float Angle1; public float Angle2; public GameObject Dragon; public float speed; public States currentState; public enum States { flying, diving }
void Update() // Changes too diving/flying State depending on the degree angle.
{
//print($$anonymous$$athf.DeltaAngle(320, 40));
if (currentState == States.flying)
{
$$anonymous$$athf.DeltaAngle(1080, 90);
//if( transform.rotation.eulerAngles.x < 320 || transform.rotation.eulerAngles.x < 20) // If the degree angle is less than, dive.
{
currentState = States.diving;
//rigidbody.AddRelativeForce(0,0,-speed);
Debug.Log("Now Diving");
rigidbody.useGravity = true;
Dragon.animation.Play("dive");
}
}
if (currentState == States.diving) // Returns to the flying state and plays the animation.
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
//if( transform.rotation.eulerAngles.x > 250 || transform.rotation.eulerAngles.x > 19)
//if(transform.rotation.eulerAngles.x <= 180)
{
currentState = States.flying;
Debug.Log("Now Flying");
//Dragon.animation.Play("";
Dragon.animation.Play("idle");
rigidbody.useGravity = false;
}
}
}
}
Ignore the "if( transform.rotation.eulerAngles.x > 250 || transform.rotation.eulerAngles.x > 19)" That's the place where I need it read the rotation properly.
Do this: remove your enumerations and replace it with a bool. Let's call it "diving." At the beginning of your code do this:
diving = ($$anonymous$$athf.Abs($$anonymous$$athf.DeltaAngle(transform.eulerAngles.x,Angle1))
Two things to note: you don't need to use transform.rotation.eulerAngles; transform.eulerAngles works fine. The next is that ins$$anonymous$$d of using a $$anonymous$$imum and maximum angle, this uses a "center" or "mean" angle and a range angle. So, for example, if you want to check if the angle is between 20 and 320 degrees, Angle1==0 (the middle/mean of 320 and 0) and Angle2==20 (the maximum angle to check from Angle1.) If the range was 180-270 then Angle1=215 and Angle2=45. If the range was 100-300 then Angle1=200 and Angle2=100. Etc.
Next change your (CurrentState == States.flying) to (!diving) and your (CurrentState == States.diving) to (diving) This saves a tiny bit of memory (every little bit counts) and simplifies the checking of the angle. Let me know how this works for you!
Answer by Berenger · Feb 19, 2012 at 10:59 PM
If you have a vector in the middle of that angle, and those at the extrimities, you can use Dot to know if it's above or below an angle. Something like (they must be normalized)
float angleDegree = Mathf.Asin( Vector3.Dot( forward, V ) ) * Mathf.RadToDeg;
That bit of codes confusing me a little bit, how do I define the two angles I want it to read?
It depends how your two angles are defined. Do you have vectors and want to know how many degree the rotation from one to the other would take (Vector3.Angle), do you have two angles, numbers, and want to know the difference (angle1- angle2) ? I don't see a third one right now. The code I gave is for something like "forward is the direction the character look, V is the direction toward the target, is the target in the character field of view ?" That might not be what you're looking for at all though.
This is exactly what I need, but how would I be able to convert it from a debug log to an if statement without errors? The main reason I want it is because if it's between the two angles, it will change states
if ($$anonymous$$athf.DeltaAngle(Angle1,Angle2) < 90) /some code here/; The above will run if the two angles are within 0-89 degrees of each other.
You can also use ($$anonymous$$athf.Abs(Angle1-Angle2)<90) to achieve a similar effect The difference in the use of Abs vs DeltaAngle is that Abs will not wrap an angle above 360 or below 0. You can, of course, us the Abs method for things besides angles.
Your answer
Follow this Question
Related Questions
Problem with "if or" statement. 1 Answer
Need help in setting a game-object's rotation to euler-degrees. 0 Answers
0-360 Y degree from Vector3.Angle 1 Answer
Rotation: rotate by a defined angle 1 Answer
Snap object rotation 1 Answer