- Home /
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! :)
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));
}
}
}
Answer by wibble82 · Apr 28, 2014 at 05:32 PM
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:
http://answers.unity3d.com/questions/659413/need-tutorials-for-rigidbodyaddforce.html
Have you tried a ridiculously high number for the force, like a million million? Just to see if its having an effect?
Thank you for the quick reply! I checked that I had set the various objects to have the right tags and layer groups. Then I tried changing the value of jumpForce in the script to a million, and since I still didn't see any changes in the scene, I decided to inspect the Player object while running the game. Turns out I had input a value out in the Inspector, which was overriding the value in the script! /facepalm I will bookmark your other explanation of AddForce for future refence. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613143808im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Character jumping at random heights. What in my code is causing this? 1 Answer
Even / Constant Speed and Jumping 2D 1 Answer
Have a delay after each jump, so user cant spam jump 3 Answers
Making a sphere jump on specific surfaces only 4 Answers
How to check for the ground in Unity 2D for Jumping 1 Answer