Character jump issue?

Hello, I’v been coding in C# for about 2 weeks, I’m planning on making a game where the player needs to adapt to challenges there faced with (No jump, no gun, etc) but obviously for this I need my character to jump, I’v been trying to figure out why my code isn’t working for ages but I just can’t figure it out, can anyone please tell me what I’m doing wrong? (And I know the code is simple)

using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {

public float moveSpeed = 5;
public float rotateSpeed = 180;
public float jumpSpeed = 20;
public float gravity = 9.8f;
CharacterController controller;
Vector3 currentMovement;

void Start ()
{
controller = GetComponent ();
}

void Update ()
{
transform.Rotate(0, Input.GetAxis (“Horizontal”) * rotateSpeed * Time.deltaTime, 0);
currentMovement = (new Vector3 (0, currentMovement.y, Input.GetAxis (“Vertical”) * moveSpeed));
currentMovement = transform.rotation * currentMovement;

if (!controller.isGrounded)
currentMovement -= new Vector3(0, gravity * Time.deltaTime, 0);
else
currentMovement.y = 0;

if (controller.isGrounded && Input.GetButtonDown (“Jump”))
currentMovement.y = jumpSpeed;

controller.Move (currentMovement * Time.deltaTime);
}
}

If anyone can help me please do. :slight_smile:

Please use code tags when posting code.

1 Like

Also, explain what isn’t working. Are you hitting the jump button and nothing happens? Is your character stuttering but not leaving the ground?

I would remove the gravity check for now and take things one step at a time. Just make it so that pressing jump propels the character up. Once that is working, add in gravity to bring him back down.

My problem is my character won’t jump when I press the jump, I also used Debug.log to test if the if statement (if (controller.isGrounded && Input.GetButtonDown (“Jump”))) is working and it is, I will try getting rid of the gravity code though.

Edit: Nope, it didn’t work, I tried removing the gravity code and just setting the variable to 0 but none worked. Thanks for trying though :slight_smile:

Your code:

if (!controller.isGrounded)
currentMovement -= new Vector3(0, gravity * Time.deltaTime, 0);
else
currentMovement.y = 0;

if (controller.isGrounded && Input.GetButtonDown ("Jump"))
currentMovement.y = jumpSpeed;

As soon as you hit the button, you’ll take off for one (or a few) frames until ‘isGrounded’ will return false, then it’ll run into the first if again and your jumpSpeed will no longer apply.

*edit nvm, didn’t see the ‘minus’, my bad

I tried removing the ‘isGrounded’ check from the if statement but it doesn’t make a difference, I also removed the whole gravity code (!controller.isGrounded) but it didn’t do anything either.

Sorry, forget what i just wrote. I just saw there is a minus so that should work properly. -_-
I’ll try your code in a second.

Ok, thanks. I seriously am starting to think it isn’t to do with my code but with untiy. I guess we’ll find out if it works for you.

Well, okay. It actually works, but not always.

isGrounded toggles all the time, you may want to add a small gravity even though you’re grounded to be sure the ground collision is detected appropriately.

If your if statement is firing correctly,
then the only problem would be with

currentMovement.y = jumpSpeed;|

Does jump speed = 0 by accident?

Ok.Thanks

Check your Character Controller’s Min Move Distance.

Since you only add movement to y in one frame (GetButtonDown() only returns true one frame): if you have a decent FPS you’ll get a low y movement (Time.deltaTime will be a low value) and so Min Move Distance might prevent your move.

That’s a good point, could try GetButton instead of just GetButtonDown, so it fires multiple frames while you hold space.

I’v been experimenting and I found when I put my character in the floor (Without gravity) and pressed space it jumped, so I think it’s something to do with the ‘isGrounded’ detection. When I put gravity on and looked closely I could see a small space between my character and the floor, any way to fix this?

Edit: I tried the remove graviy and put character in floor thing again and it didn’t work, I’m just plain confused now.

That might be caused by the skin width of the CharacterController, but you shouldn’t make it too thin afaik.
Anyway, does it still not jump at all or just sometimes?

It still doesn’t jump at all, the one time where it did jump (When there was no gravity and it was in the floor) it only jumped sometimes.

I changed the height of the collision box to 1.9 instead of 2 which seems to have fixed the problem of it not jumping at all and I can just model my character around that collision box, however it doesn’t always jump.

Well sounds like the isGrounded detection isn’t working properly, personally I never use the controller.isGrounded because I found it to be finicky. I reccommend shooting a ray out of the bottom like this and seeing if the dist is within a certain tolerance.

groundDist can just be a public float you set in the inspector.

void checkGround()
{
     RaycastHit hit;
  
     if(Physics.Raycast (transform.position, Vector3.down,out hit, Mathf.Infinity))
     {
       if(hit.distance < groundDist)
       {
         grounded = true;
       }
       else
       {
        grounded = false;
       }
     }
}

Have you then tried to apply a small gravity instead of 0 in your ‘else’?

Thanks everyone for your help, my code works now :slight_smile:

Here’s the code in case anyone has the same problem or is interested:

void Update ()
{
transform.Rotate(0, Input.GetAxis (“Horizontal”) * rotateSpeed * Time.deltaTime, 0);
currentMovement = new Vector3 (0, currentMovement.y, Input.GetAxis (“Vertical”) * moveSpeed);
currentMovement = transform.rotation * currentMovement;

if (!controller.isGrounded)
currentMovement -= new Vector3(0, gravity * Time.deltaTime, 0);
else
currentMovement.y = -0.5f;

if (controller.isGrounded && Input.GetButtonDown(“Jump”))
currentMovement.y = jumpSpeed;

controller.Move (currentMovement * Time.deltaTime);
}
}