My Jumping Script is not working and Gravity bit broken

Can someone maybe see what´s the issue in my code? BTW code by Brackeys but maybe its outdated cause its like 1 year ago. Ive looked multiple times throu but just cant find anything please help if you can. I cant jump when im pressing “Space”. But everything else like looking around and walking works but gravity and Jumping is kinda bad. When i jump down from something i instandly hit the ground. Someone maybe got and script which would be good here?

using System.Collections;
 
using System.Collections.Generic;
 
using System.Net.Mime;
 
using System.Security.Cryptography;
 
using System.Threading;
 
using UnityEngine;
 

 
public class Moving3D : 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);
 
    }
 
}

Did you try increasing jumpHeight? Line 80 should result in a value of about 8. That’s less than your gravity value, so any jump force will be cancelled out in less than a second by gravity. That should still be noticable tho.

Did you set values for groundCheck and groundMask in the inspector?
Are there any warnings or errors in your console? You need to fix these before expecting anything to work.

As a piece of general advice, Debug.Log is your friend. Using that you can easily print some values from your code and thus find the problem. If the above does not fix your problem, put some Debug.Log()s in your code to check values, compare them to expected values and see if, for example, if statements get entered, or why not. You could start by printing your velocity vector. Or maybe isGrounded is not true when you expect it to be. Debug.Log will tell you.

If you really just want a working script… there are plenty of examples out there. However, as you rightfully said in your other thread, you should start learning at 0 and improve from there - and skipping something because you have problems with it is the opposite of that. I would also recommend the youtube tutorial series on Unity Gamedev by Sebastian Lague. It’s really well made and tailored to beginners. The same guy also has a nice series on designing a character, from modeling it to making it walk, run, jump, … and so on.

Allright first of thank you verry much for always responding so fast ^^ and second, i have done it and i can clearly see that the if statement with the “Jump” button is not working

Nice! Because of you i found out that the jumping script was fine just the isGrounded one was broken ive fixed it with this now :slight_smile:

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class Moving3D : 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;

    void Start()
    {
        controller = GetComponent<CharacterController>();
    }


    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if(isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        if (Input.GetKeyDown("space") && controller.isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }


        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);


        velocity.y += gravity * Time.deltaTime;

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

thanks for your help!