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;
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
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.
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.
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.
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.
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?
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.