Jump Script Not working (3D)

I’ve been trying to get this C# jumping script to work, but I can’t. The script was originally for a 2D game, but the only thing I changed was turning 2D Rigidbodies into 3D ones and replacing the Overlapcircle used for checking if the player is grounded with a sphere. The compiler and the engine both say the script is alright, but when I press the jump key, nothing happens.

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Security.Cryptography;
using UnityEngine;

public class PlayerJump : MonoBehaviour
{

    public float jumpForce;

    private bool isGrounded;
    public Transform feetPos;
    public float checkRadius;
    public LayerMask whatIsGround;

    private Rigidbody rb;

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.OverlapSphere(feetPos.position, checkRadius, whatIsGround).Length > 0;

        if (isGrounded == true && Input.GetKeyDown(KeyCode.Space));
        {
                UnityEngine.Debug.Log("jump");
                rb.velocity = Vector3.up * jumpForce;
        }
    }
}

Thanks in advance :slight_smile:

I looked through your code and it seems you need to make sure you get ahold of the rigidbody. and you need to make sure that you ‘checkRadius’ is sufficient. I have refactored your code, perhaps it can help you understand it a bit better.

Also, it is not necessary to be checking for grounded all the time, only when you need it, so I made property for it.

I have furthermore added an OnDrawGizmos so that you can see if your check radius is enough to actually check for the ground.

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Security.Cryptography;
using UnityEngine;

// make sure this GameObject has a rigidbody as it is necessary for this script to work
[RequireComponent(typeof(Rigidbody))] 
public class UnityAnswers : MonoBehaviour
{
    [SerializeField] float jumpForce;
    [SerializeField] Transform feetPos;
    [SerializeField] float checkRadius;
    [SerializeField] LayerMask whatIsGround;
    private Rigidbody rb;

    public bool IsGrounded
    {
        get { return (Physics.OverlapSphere(feetPos.position, checkRadius, whatIsGround).Length > 0); }
    }

    private void Start()
    {
        rb = GetComponent<Rigidbody>(); // we know this will always work because at the top we have required this component
    }


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if(IsGrounded)
                rb.velocity = Vector3.up * jumpForce;
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(feetPos.position, checkRadius);
    }
}

Hey there , if anyone has the same problem. Try to change the layer to “Ground”. Once I made a prototype everything was working fine. Then I changed my assets with new models and didn’t set the layer as Ground.