- Home /
Other
How Can I Reference A Range e.g. Between -90 & 90
I want to test whether my car is between a certain angle by using localEulerAngles but i dont know how to make it so that i can test for a range.
function Update()
{
carPhysicsUpdate();
if (Grounded && PlayerCar.localEulerAngles.z == //Between -90 & 90 degrees)
{
checkInput();
}
if (Input.GetKeyDown(KeyCode.Return))
{
if (Grounded)
{
PlayerCar.localEulerAngles.y = 0;
PlayerCar.localEulerAngles.x = 0;
PlayerCar.localEulerAngles.z = 0;
}
}
}
Answer by robertbu · Feb 20, 2014 at 05:23 PM
Let's start with the easy stuff...the answer to the explicit question you asked:
var zAngle = PlayerCar.localEulerAngles.z;
if (Grounded && ((zAngle >= 0 && zAngle <= 90) || (zAngle >= 270 && zAngle <= 360))) {
Note that localEulerAngles produces a value in the range of 0 to 360, so you have to check 0 - 90 and 270 - 360 independently.
The bad news is that this code may not work. It will likely work if the object is only being rotated on the 'z' axis. If you are doing arbitrary rotations on all axes, then you likely have to look to a more sophisticated solution. What solution will depend on the code you are using for the rotation of this object.
Note that lines 12 - 14 can be replaced by:
PlayerCar.localEulerAngles = Vector3.zero;
your code works but sadly my z angle of my car goes to $$anonymous$$us digits so when it is in that range my car doesn't drive, i really need a way of making it between 90 & -90 if you can
If your localEulerAngles is producing negative angles, you can do:
if (Grounded && (zAngle >= -90 && zAngle <= 90)) {
okay so it appears that the angles i need are between (0 & 90) and (270 & 360) but sometimes it goes to $$anonymous$$us when on flat ground so how could i have this:
if (Grounded && ((PlayerCar.localEulerAngles.z >= 0 && PlayerCar.localEulerAngles.z <= 90) || (PlayerCar.localEulerAngles.z >= 270 && PlayerCar.localEulerAngles.z <= 360)))
And then included $$anonymous$$us numbers aswell, so between (0 & -90)
The easiest thing to do would be to normalize the result:
var zAngle = PlayerCar.localEulerAngles.z;
if (zAngle < 0.0)
zAngle = zAngle + 360.0;
if (Grounded && ((zAngle >= 0 && zAngle <= 90) || (zAngle >= 270 && zAngle <= 360))) {
Thanks i managed to get it working using this:
if (Grounded && ((PlayerCar.localEulerAngles.z >= 0 && PlayerCar.localEulerAngles.z <= 50) || (PlayerCar.localEulerAngles.z >= 310 && PlayerCar.localEulerAngles.z <= 360) || (PlayerCar.localEulerAngles.z <= 0)))
your suggestion glitched out thanks anyway.