- Home /
C# Unity IF Statement not working properly and I can't figure out why. Deals with basic rotation stuff.
So, I was working on my project and making a simple IF statement that performs certain functions based up the current Z rotation (in Euler Angles) of the object that the script is attached to. But I ran into a problem, which is demonstrated in the "BulletSponge()" method below.
void Update () {
ConductMovement();
BulletSponge();
CheckForFiring();
DecrementFiringTimer();
Debug.Log ("Rotation: " + transform.eulerAngles);
}
void BulletSponge(){
float BOB = gameObject.transform.eulerAngles.z;
if (BOB == 180){
Debug.Log ("WIN");
}
Debug.Log ("BOB: " + BOB);
}
The problem is that both the "Debug.Log ("Rotation: " + transform.eulerAngles);" and "Debug.Log ("BOB: " + BOB)" say that "gameObject.transform.eulerAngles.z;" is equal to 180 in the Console Log, yet the IF statement won't fire (The "WIN" message never shows up.) Anyone have any ideas? I appreciate any help.
Answer by KiraSensei · May 21, 2013 at 08:56 AM
Try Mathf.Approximately()
You are comparing a float with an int. Approximations can be a problem here.
That Worked. Thanks a ton. I guess I don't completely understand how floats work. Why is it not exactly 180, yet my outputs give me exactly 180 or 180.0?
short answer int = 180, float = 179.999999999f but sometimes read as 180f.
also if $$anonymous$$ira's Answer helped I suggest you show that by Ticking $$anonymous$$ira's Answer
There are always approximations with floats.
In your example, maybe your outputs are rounding the number before showing it to you.
Everytime you want to compare a float with something else (a float or an int), you need to think about approximations errors. That's the problem with an exact science :)
Thanks. $$anonymous$$ath is so weird... I have a love/hate relationship with it.
EDIT: Not letting me tick $$anonymous$$ira, says I need to login as another user. I'll figure it out. Sorry, I'm new here. The thumbs up isn't working :( I'll get it/ Thanks people :P
If I remember correctly, you can tick the answer anytime, but you have to wait for a high enough karma to thumbs up.
Answer by Fornoreason1000 · May 21, 2013 at 08:48 AM
how are you getting BOB to equal 180? are you sure it reaches Exactly 180? if the game_object is the player, you will have a very hard time getting it to equal 180 without hard coding it. try this
if(BOB > 179 && BOB < 181)
or you can cast BOB as an int then back to a float
BOB = (float)((int)gameObject.eulerAngles.z)
hope it helps :)
Answer by bls61793 · May 21, 2013 at 09:34 AM
I have it set exactly to 180 because that is the default rotation that my object has. The original idea is to have a set of IF statements handling 8 different degree angles caused by the following code:
void HandlePlayerMovementandRotation(){
//Case Statements Handling Player Movement and Rotation
if (Input.GetKey(KeyCode.A) == true){
transform.Translate( -4f * Time.deltaTime, 0, 0,Space.World);
//Check if Backward Movement is Enabled
if (CharFaceOppositeMovement == true){
transform.rotation = Quaternion.Euler(0,0,90);
}
else{
transform.rotation = Quaternion.Euler(0,0,-90);
}
}
if (Input.GetKey(KeyCode.D) == true){
transform.Translate( 4f * Time.deltaTime, 0, 0,Space.World);
//Check if Backward movement is enabled
if (CharFaceOppositeMovement ==true) {
transform.rotation = Quaternion.Euler(0,0,-90);
}
else{
transform.rotation = Quaternion.Euler(0,0,90);
}
}
if (Input.GetKey(KeyCode.W) == true){
transform.Translate( 0, 4f * Time.deltaTime, 0,Space.World);
if (CharFaceOppositeMovement == true){
transform.rotation = Quaternion.Euler(0,0,0);
}
else{
transform.rotation = Quaternion.Euler(0,0,180);
}
}
if (Input.GetKey(KeyCode.S) == true){
transform.Translate(0, -4f * Time.deltaTime , 0,Space.World);
if (CharFaceOppositeMovement == true){
transform.rotation = Quaternion.Euler(0,0,180f);
}
else{
transform.rotation = Quaternion.Euler(0,0,0);
}
}
}
void HandleAnglesDuringDiagonalMovement(){
//Expressions Handling angles during Diagonal Movement
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A) == true){
if (CharFaceOppositeMovement == true){
transform.rotation = Quaternion.Euler(0,0,45f);
}
else{
transform.rotation = Quaternion.Euler(0,0,-135f);
}
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) == true){
if (CharFaceOppositeMovement == true){
transform.rotation = Quaternion.Euler(0,0,-45f);
}
else{
transform.rotation = Quaternion.Euler(0,0,135f);
}
}
if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A) == true){
if (CharFaceOppositeMovement == true){
transform.rotation = Quaternion.Euler(0,0,135f);
}
else{
transform.rotation = Quaternion.Euler(0,0,-45f);
}
}
if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D) == true){
if (CharFaceOppositeMovement == true){
transform.rotation = Quaternion.Euler(0,0,-135f);
}
else{
transform.rotation = Quaternion.Euler(0,0,45f);
}
}
}
I forgot to mention, my game is 2D. My character is a sprite pasted on a thin cube mesh. The cube mesh starts at a rotation of 180*. The default rotation is 0* facing the bottom of the screen.
Your answer
Follow this Question
Related Questions
Really weird problem with if statements [C#] 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Simple question about targetting 0 Answers
Clamp an object on 2 sides 0 Answers