How To Check isGrounded Or Not PLEASE HELP
I have a C Sharp script and I got it to jump. But what I found out is it double jumps, triple jumps, there is no limit to how much you jump! Can you help me?
using UnityEngine;
using System.Collections;
public class PlayerJump : MonoBehaviour {
public float jumpForce = 800f;
private bool jumping = false;
private Rigidbody2D rb;
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
void Update()
{
if (Input.GetKeyDown (KeyCode.Space) && !jumping)
{
jumping = true;
}
}
void FixedUpdate ()
{
if (jumping)
{
rb.AddForce(new Vector2(0,jumpForce));
jumping = false;
}
}
}
I tried to add the variable "jumping" to stop this but it didn't help.
Answer by SneakySquid · Jan 17, 2016 at 04:40 AM
You need some kind of "isGrounded" flag and only allow jumping when grounded. This can easily be done with a CharacterController, but if it has to be a Rigidbody2D, then I'm not sure.
I'm not sure how to activate and deactivate the isGrounded bool. I'm not good with stuff colliding. So does anyone know if I can use an isTrigger Box Collider? @Dudicus
Your answer
Follow this Question
Related Questions
Sprite 2d passes through the ground 0 Answers
OnTriggerEnter2D executed once 0 Answers
Collision or movement Bug (2D game dev) 1 Answer
,Doesn't stop adding force to enemy 0 Answers