False = true, what am I doing wrong?

Guys I’m having trouble enabling and disabling a jump. It all worked fine until I added a horizontal movement controller, and now even if I remove it it still doesn’t work. I can’t find a logic problem and I’ve re-read my code so many times. Please help me find the issue.

using UnityEngine;
using System.Collections;

public class Bouncer : MonoBehaviour {

	public float jumpForce;
	public float moveSpeed;
	public int jumpAttempts = 5000;
	public int totalJumps;
	public int successfulJumps;






	private bool jump = false;

	void OnTriggerExit2D (Collider2D wall)
	{
		if (wall.tag == "Border")
		{
			jump = false;
			Debug.Log ("Exited");
		}
	}
	
	
	void OnTriggerStay2D (Collider2D wall)
	{
		if (wall.tag == "Border")
		{
			jump = true;
		}
	}


	void Update ()
	{
		// Change world axis to local axis.
		Vector3 direction = transform.InverseTransformDirection (Vector3.up); 

		// Set the jumping controls.
		if (Input.GetButtonDown ("Jump") && jump && jumpAttempts > 0)
		{
			rigidbody2D.AddForce (new Vector2(direction.x,direction.y) * jumpForce * successfulJumps);

			// Configure jump rules.
			jumpAttempts = jumpAttempts - 1;
			successfulJumps = successfulJumps + 1;
			totalJumps = totalJumps + 1;

			jump = false;

			Debug.Log ("Jumps Left:"+jumpAttempts);
		}

		// Punish the player for failure.
		if (Input.GetButtonDown ("Jump") && !jump && jumpAttempts > 0)
		{
			jumpAttempts = jumpAttempts - 1;
			successfulJumps = 1;
			Debug.Log ("Killed Jump, counter reset to" + successfulJumps);
		}

	}



	void FixedUpdate ()
	{
		float horizontalInput = Input.GetAxis ("Horizontal");

		if (Input.GetAxis ("Horizontal") != 0)
		{
			rigidbody2D.AddForce (new Vector2 (horizontalInput,0) * moveSpeed);
		}
	}

}

With this code, if my player enters a trigger for the first time, jump is enabled, but then its disabled even if he stays in the trigger. It seems the false fires off during true, please tell me where is the problem, I’m almost ready to blatantly copy → paste the character controller from Unity’s 2D platformer…

Your logic error is probably inside Update. The two if statements (line 44 and line 59) are always simultaneous true. Why? well that’s simple:

First of all both if statements are executed in a row. Both need jumpAttempts to be greater than 0 and the jump button pressed. The only difference is that the first needs “jump” to be true while the second needs “jump” to be false. The problem arises from the line 53 inside the first if’s body where you set “jump” to false.

That means the second if is always true when the first is true. To shorten what happens look at this equivalent of your code:

if (Input.GetButtonDown ("Jump") && jumpAttempts > 0)
{
    if (jump)
    {
        //[first if body]
        jump = false;       // this makes the second if also true.
    }
    
    if (!jump)
    {
        // [second if body]
    }
}

This is what happens in your code. Since the jump is set to true every frane inside TriggerStay jump is always true when you enter Update. So both if-statements are executed at once.

Are you sure you want OnTriggerStay2D? Keep in mind it’s executed every frame as long as you’re inside the trigger.

It’s difficult to understand what this is all about. It looks like some kind of wall-jumping. A short explanation would be nice :wink:

I guess you might want to use my shorten version of your if construct but use an else like this:

if (Input.GetButtonDown ("Jump") && jumpAttempts > 0)
{
    if (jump)
    {
        //[first if body]
        jump = false;
    }
    else
    {
        // [second if body]
    }
}

Its very simple bug if i am getting your problem in your code.

In,
void OnTriggerStay2D (Collider2D wall)
{
if (wall.tag == “Border”)
{
jump = true;
}
}
you just need to add else part…

void OnTriggerStay2D (Collider2D wall)
{
if (wall.tag == “Border”)
{
jump = true;
}
else
{
jump = false;
}
}
According to me this should work perfectly.