- Home /
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...
What does your logs say? Is "Exited" printed even if the player stays in the trigger? Also, a tip is to set jump to a public variable in order to easier see its value changing in the inspector window.
Answer by Bunny83 · Mar 31, 2014 at 01:53 PM
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 ;)
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]
}
}
Ahhh I can't believe it was staring me in the face. Yes you are right, thanks.
Yeah its a wall jumping thing, but ultimately I ended up scrapping this controller and opted out for a Physics2D.OverlapCircle collider check in order to enable jumps. Nevertheless thank you for the answer, I can't believe I didn't realize it myself :)
Answer by NDJoshi · Mar 31, 2014 at 01:22 PM
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.
That wouldn't help much. OnTriggerStay2D is only called when you actually touch / intersect a trigger. Once he left the "Border" OnTriggerStay2D isn't called anymore unless he's intersecting another trigger. OnTriggerExit2D is the proper way to deter$$anonymous$$e that you don't intersect anymore.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
C# Character Controller Collision If Statement 2 Answers
Check if all values in a list are equal 1 Answer
Multiple Cars not working 1 Answer