If statement is being ignored (150157)

Hi,

Somehow this part of my code is being ignorded: && Difference < 0.5f
When I use == instead of > it functions properly.

void Update ()
              {
		float Difference = player1.transform.position.x - transform.position.x;
			if (Input.GetKeyDown (KeyCode.Space) && Difference > 0.5f) 
			{
                                Debug.Log (player1.transform.position.x);
				Debug.Log (transform.position.x);
				Debug.Log (Difference);
                         }
               }

The Debug.Log (Difference) shows that when Difference > 0.5f the if statement still is ‘true’.

Can somebody please help me with my problem?

Thanks in advance!

Have you checked that the script is not attached to another object in your scene? Maybe the "if" is true for that object. Add a line like the next one to make sure it happens in the object you think it's happening: Debug.Log(name);

–

1 Answer

1

The Debug.Log (Difference) shows that
when Difference > 0.5f the if
statement still is ‘true’

Isn’t that what you have coded? “If space is pressed and Difference > 0.5f then do something”. If the log is showing that Difference is > 0.5, then “Difference > 0.5)” is going to be true every time.

Sorry, was a typo in my question on this site. In my script it is: && Difference < 0.5f

–