Jump Script, how to check if on ground.

Hey,

After what seems a lot of work, I finally got a jumping script. The issue is that after I press the jump button, I can simply hold or keep tapping the button therefore making me higher and higher.

A google showed things such as ‘IfGrounded’, ‘Raycasting’ and ‘Boolens’ but I simply wasn’t able to get any of those systems, so I’m asking for help here on how I should make this work. So how can I make it only jump, if my player is on the ground. The ground never changes height, so that could be an advantage?

Here’s the code I currently have… Thanks in advance!

using UnityEngine;
using System.Collections;

public class Jump : MonoBehaviour {

	//Controls Player Jump Height
	private int jumpHeight = 25;
	
	void Start () {
	
	}

	void Update () {

		if (Input.GetButtonDown("Jump") || Input.GetKey ("w")) {
			DoJump();
		}
	
	}
	
	void DoJump (){
		rigidbody2D.AddForce (new Vector2(0, jumpHeight));
	}

}

You need something like this

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        CharacterController controller = GetComponent<CharacterController>();
        if (controller.isGrounded)
            print("We are grounded");
        
    }
}

Just set a tag with any item you want to be classed as the ground the detect a colision

void OnCollisionEnter(Collision col)
	{
		if(col.gameObject.tag == "Ground")
		{
			isGrounded=true;

Do the opposite for when you exit the collision

void OnCollisionExit(Collision col)
	{
		if(col.gameObject.tag == "Ground")
		{
			isGrounded=false;

Then only allow your jump routine to work if isGrounded == true

hi You should use Physics2D.OverlapCircle

link to docs Unity - Scripting API: Physics2D.OverlapCircle

link to unity example http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers