C#: Jump function, grounded stays True

Sorry if I’m re-posting an issue or doing something wrong, this is the first time I’m asking a question here.
I’m fairly new to C# and Unity, so I’ve followed a tutorial in order to get my character controls in order. As far as I can tell, I’ve done as shown in the tutorial: 2D Character Controllers

When my Player drops down and hits the Ground, the grounded variable stays True. When I press Space to jump, the jump sound is played, so the Jump if-statement is run, but the Player object does not jump, at least not as far as I can see in the game. I increased the float variable for jumpForce to 700 to see if it simply needed more force, as I’ve set the gravity in the game to -30 as the tutorial said… Moving from side to side works, but the jump isn’t. And the grounded variable stays True, but I can still press the Space and hear the jump sound every time I press it.

I’ve tried looking at another post about this issue, but it hasn’t helped me: can’t jump in 2d?
Please tell me if there is something blatantly obvious that my untrained eyes have overlooked! :slight_smile:

Here is my code:

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour

	public float maxSpeed = 10.0f;
	public float jumpForce = 700.0f;
	private bool grounded = false;			// Whether or not the player is grounded.
	public Transform groundCheck;			// A position marking where to check if the player is grounded.
	float groundRadius = 0.2f;
	public LayerMask whatIsGround;

	float minValX = -6.5f;
	float maxValX = 6.5f;

	public AudioClip Jump1;

	void FixedUpdate (){

		grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);

		Debug.Log (grounded);

		float move = Input.GetAxis ("Horizontal");

		rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
		if (rigidbody2D.transform.position.x < minValX)
			rigidbody2D.transform.position = new Vector3 (minValX, transform.position.y, transform.position.z);
		if (rigidbody2D.transform.position.x > maxValX)
			rigidbody2D.transform.position = new Vector3 (maxValX, transform.position.y, transform.position.z);

	void Update(){
		if(grounded && Input.GetKeyDown(KeyCode.Space)){
			Debug.Log ("Jump!");
			AudioSource.PlayClipAtPoint(Jump1, transform.position);
			grounded = false;
			rigidbody2D.AddForce(new Vector2(0.0f,jumpForce));

Hi there

Your code looks roughly right. Your grounded variable may be staying true continuously as the Physics2D.OverlapCircle tests if there are any objects in the circle at groundCheck.position, with a layer mask that matches whatIsGround. Hence you’ll need to make sure the groundCheck transform is setup correctly (presumably its supposed to be the transform of an object attached to the players feet), and the layer mask needs to match the ground’s layer.

The correct layer mask to use would probably be something like:

//get the layer number of the ground
int ground_layer_number = LayerMask.NameToLayer("Ground")

//create a 'mask' - a 32 bit value with a bit set for each layer to test. 
//In this case we only want to check for the ground layer so we use 1 shifted left by ground_layer_number bits
whatIsGround = 1 << ground_layer_number;

In terms of the force code, it looks sensible (assuming that “Jump” is being printed out at the correct time and you aren’t seeing any exceptions). However even a force of 700 may be too low! Forces are scaled by the mass of the object AND the time step. Impulses are a better bet for 1 shot forces like this. Check out my tutorial on forces for a bit more info on that:


Have you tried a ridiculously high number for the force, like a million million? Just to see if its having an effect?