Jump code not working

so, I tried following a tutorial on how to make your character jump, but for some reason, I press the space key and nothing happens, I’m pretty new to gamedev, so it 100% can be my fault, but I just cant see what I’m doing wrong
here is the full movement code:

using System;
using UnityEditor.Callbacks;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;

public class ThirdPersonMovement : MonoBehaviour
{
    public CharacterController controller;
    public Transform cam;

    public float speed = 20f; // Max speed
    public float acceleration = 5f; // Rate at which speed increases
    public float turnSmoothTime = 0.1f;
    float turnSmoothVelocity;

    public float playerHeight;
    public LayerMask whatIsGround;
    
    bool grounded;
    public KeyCode jumpKey = KeyCode.Space;
    public float jumpForce;
    public float jumpCooldown;
    public float airMultiplier;
    bool readyToJump;
    Rigidbody rb;

    float currentSpeed = 0f; // Current speed, starts at 0

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.freezeRotation = true;
        UnityEngine.Cursor.lockState = CursorLockMode.Locked;
        UnityEngine.Cursor.visible = false;
        
        readyToJump = true;  // Initialize readyToJump to true
    }

    void Update()
    {
        grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);

        MyInput();  // Add this line to call the MyInput method

        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

        if (direction.magnitude >= 0.1f)
        {
            // Calculate target angle based on movement direction
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);

            // Gradually increase speed based on acceleration
            currentSpeed = Mathf.MoveTowards(currentSpeed, speed, acceleration * Time.deltaTime);

            // Move the player in the direction of the camera
            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * currentSpeed * Time.deltaTime);
        }
        else
        {
            // If no input, stop moving smoothly
            currentSpeed = Mathf.MoveTowards(currentSpeed, 0f, acceleration * Time.deltaTime);
        }
    }

    private void Jump()
    {
        rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);  // Reset y velocity before applying the jump force
        rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    }

    private void ResetJump()
    {
        readyToJump = true;
    }

    private void MyInput()
    {
        if (Input.GetKey(jumpKey) && readyToJump && grounded)
        {
            readyToJump = false;
            Jump();
            Invoke(nameof(ResetJump), jumpCooldown);
        }
    }
}

any help is appreciated, thanks:]

1 Like


Here was one I used for the same tutorial then built off it -(Player does jump)

Seems like your code has a few issues going on with conflicts.
IE- CharacterController being used for movement but Rigidbody used for jump.
Try changing “rb.linearVelocity” to rb.velocity
Groundcheck- relies on accurate configuration of playerHeight and whatIsGround - If these values are incorrect (IE ray length is too short or whatisground not set correctly)

In the code I used -player is able to jump even mid air (but does ground itself on one jump/will crash through floor on multiple- First step is just getting it off the ground-

“Jump” input is mapped by the spacebar by default in Unity - so no need for a custom key code

You got this!

so, if I only put in the rb.AddForce(Vector3.up thing in void update, it does get me off the ground, but I start levitating, I have to assume this is because gravity is turned off, but if I turn it on, I phase off the ground, sooooo… it isnt really working, thanks tho!

If you’re phasing through the ground, I assume the issue is with collision detection in some way, in the script properties in Unity, is your “What Is Ground” layer set to the layer of what you want to land on? try setting it to “Everything” and see if the phasing still happens