I only want my character to jump when touching the ground

I am new to unity and C# and I got this far. and was wondering if someone could help me with the code for this last bit.

What I have is that the character will jump and jump as soon as he touches the ground due to the

“if collider.collision.tag == “Ground””
then the code down here to make him jump.

is there a way I can make it Only jump IF he is on the ground and I hit the spacebar/Mouse

Thanks in advance here is the code!

using UnityEngine;
using System.Collections;

public class Running : MonoBehaviour {

	public float acceleration;

	public Score score;

	private float seconds = 1;

	Animator animator;
	
	public bool dead = false;
	float deathCooldown;

	public Vector3 jumpVelocity;
	

	// used to get animations
	void Start () {



		animator = transform.GetComponentInChildren<Animator>();

		if (animator == null) {
						Debug.LogError ("Didn't find animator!");
				}
	}
	
	
	
	// Update is called once per frame
	void Update () {
		if (dead) {	

			seconds -= 1 * Time.deltaTime;
						if (seconds <= 0) {
								Application.LoadLevel ("DeathScene");

						}
				} else {
						if(Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) {
						
						}
				}
	}
	

	void FixedUpdate() {
		if (dead)
						return;
		//Run Speed
		transform.Translate (4.2f * Time.deltaTime, 0f, 0f);
	}

	//Collider to die
	void OnCollisionEnter2D(Collision2D collision) 
	{

		if (collision.collider.tag == "Obstacle" || collision.collider.tag == "ObstacleBag" || collision.collider.tag == "ObstacleGarbage") {
						animator.SetTrigger ("Death");
						dead = true;
						audio.Play();
		}

		if (collision.collider.tag == "Ground" || (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown (0))) {
			rigidbody2D.AddForce (Vector3.up * 185);
			animator.SetTrigger ("DoJump");
			
		}
	}
}

I think it’s something like this.

	public Transform groundCheck;
	bool grounded = false;
	public int jumpSpeed;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1<< LayerMask.NameToLayer("Ground"));
		if ((Input.GetKeyDown("space"))&&(grounded))
		{
			rigidbody2D.AddForce(transform.up*jumpSpeed);
			grounded = false; //so you can jump only once
		}
							
	}

You’ll make a new GameObject and place it under your Player (no collider needed on GameObject, that white thing is Player :smiley: ). Also sprite which represents ground should have layer named “Ground”.
33031-gameobject.png

in java you could use if(is.Grounded) but im not sure if you can do the same in c#

Here is a tutorial that deals with basic character movement, including jumping.

I have the same issue

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public Animator Anim;
    public Rigidbody2D PlayerRigidbody;
    public int Pulo;

    
    public bool punch;

    //verifacador de chao 
    public Transform GroundCheck;
    public bool grounded;
    public LayerMask whatisGround;
    
    //soco tempo
    public float socoTempo;
    public float tempoTempo;

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
	
        
        // se o pulo for apertado e estiver pisando no chao, faca isso
    if(Input.GetButtonDown("Jump") && grounded== true) {
            PlayerRigidbody.AddForce(new Vector2(0, Pulo));
            
        }

    if(Input.GetButtonDown("Fire2")) {
            punch = true;
            tempoTempo = 0;

        }

        grounded = Physics2D.OverlapCircle(GroundCheck.position, 0.2f, whatisGround) ;

        if(punch == true)
        {
            tempoTempo += Time.deltaTime;
            if(tempoTempo >= socoTempo)
            {
                punch = false;
            }
        }

        Anim.SetBool ("soco", punch);

	}
}