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.

Try Mathf.Approximately()

You are comparing a float with an int. Approximations can be a problem here.

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 :slight_smile:

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.