As the title says, i have a problem with a script that should allow me make a multi-jump with a JumpCount wich is going to run out after 4 jumps and recharge when i touch the ground. The script does not give any error, but i think there is something wrong directly in the script’s drawing up It simply doesen’t work when i apply it, my character jumps just one time.
using UnityEngine;
using System.Collections;
public class Volo : MonoBehaviour {
float jumpCount;
public Vector2 jumpForce = new Vector2(0, 300);
bool grounded;
void Update()
{
// If the character has more than 0 jumps left
if (Input. GetKeyDown("space") && jumpCount > 0)
{
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(jumpForce);
jumpCount--; // Reduce the amount of jumps left by 1
}
// If the character is touching the ground
if (grounded)
{
jumpCount = 4; // Reset amount of jumps left to 4
}
}
void OnCollisionEnter(Collision col) // When the character collides with something
{
if(col.collider.tag == "ground") // If the object has the tag "ground"
{
grounded = true;
}
}
void OnCollisionExit(Collision col) // When the character stops colliding with something
{
if(col.collider.tag == "ground") // If the object has the tag "ground"
{
grounded = false;
}
}
}