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…