I was just following along to Bracky's fps movement tutorial

And everything seems to work, except for jumping. For whatever reason, I can’t jump. I made sure the key input for jump was “space.” I also tried other keys, and nothing worked. I tried changing the code to

if(Input.GetKey(“space”) && isGrounded)

I also tried putting a semicolon after " if(Input.GetButtonDown(“Jump”) && isGrounded)" but all that did was make me fly up forever without any input. Unity is confusing bruh. Just looking for what’s wrong with this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayaMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = 9.81f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis(“Horizontal”);
float z = Input.GetAxis(“Vertical”);
Vector3 move = transform.right * x + transform.forward * z;

controller.Move(move * speed * Time.deltaTime);
if(Input.GetButtonDown(“Jump”) && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;

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

The most likely problem, speaking from experience, is that “isGrounded” isn’t working right.

Either make “isGrounded” public or put [SerializeField] before it so you can see it in the inspector and I bet you’ll find that it isn’t showing grounded when you expect it to.

Likely causes of this are: you need to reduce the skin width on the character controller or you need to adjust the Center and Height values of your character controller (which affects the capsule collider.). I like to use a value of 0.9Y for the center and 1.8 for the height but it may also depend on your character model.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand compiler and other errors and even fix them yourself:

https://discussions.unity.com/t/824586/8

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

I supose the rest is useful for the future but there are no errors, it simply does not work.

There’s a really good way to track this stuff down, since it really happens a lot. In fact, I do it every single day. I write some code, I fix the syntax errors, I put it on the GameObject, I run my program and … nothing happens. WTF?!

Every. Single. Day.

Remember there are like 57 billion ways this can fail, so here’s what you do:

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

You could also just display various important quantities in UI Text elements to watch them change as you playtest.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

Hello!

Gave it a quick glance but this part confuses me.

You have :

if(Input.GetButtonDown("Jump) && isGrounded)
{
  velocity.y = Mathf.sqrt(jumpHeight * -2f * gravity);
}

jumpHeight = 3.0f; gravity = 9.81f; -2f = well -2.0f;

You appear to be setting to the square root of a negative number. This results in NaN (Not a Number). I assume that after you set to NaN that the behaviour is undefined.

In your code the gravity variable is a positive number, and in your Update function it’s being added to velocity.y. Since positive along the Y axis is up, that code is going to make you fall upwards. Setting gravity to -9.81f is how it’s usually handled (setting gravity to a negative number, and adding that to the Y velocity).