Help I got this error ! i was whatching one of Brackys vids

My Jump code just wont seem to work. I need help!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

“I got this error” is completely useless. I get errors all day long in fact!

How to report problems productively in the Unity3D forums:

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

Help us to help you.

@Kurt-Dekker Wasnt meant to type “i got this error.” Its just not jumping

Excellent! We’re getting somewhere, now 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.

1 Like

Ah yes i should try that @Kurt-Dekker

Where Should i put my debug.log? and what other code do i need to put on it to make it tell me its jumping
?

You should put a debug.log into the “if(Input.GetButtonDown(“Jump”) && isGrounded)”, to see if button press is triggered. If you don´t see the log in the console, you know, that nothing happens if you press the jump button.

If that works, i would maybe check if the “isGrounded” field has the value which you expect.

@Sphinks Im not sure how im ment to do that?

In the if statement, just do

if(Input.GetButtonDown("Jump") && isGrounded)
{
Debug.Log("Jump button pressed");
velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
}

If you run the game in unity, check if you see the output “Jump button pressed” in the console.

To see, if “isGrounded” is true (what is has to be, to enter your if statements) you can put a

Debug.Log(isGrounded);

under the line 24.
You can do such logs to check each value you need or you use the debug function of visual studio to debug the code (if you use VS).

Yes I use Visual Studio Code. And now i have added the Debug.log statement when i press space (Jump) It doesnt come up in console with anyting? @Sphinks

Ok, so it never enters that if statement. Put the other log to see if “isGrounded” is true.

Just to be sure, did you add the script to your player (object) in the scene ?

Ah It is false! Its saying false, That Debug.Log(isGrounded); actually is telling me if it is false or not.

How do i make it grounded tho? the player is a capsule.

OK, so you have to look, why it is false. I don´t know what to do to solve that problem, sry. Is 0.4 the correct value for “groundDistance” ?

But you said in the title, you have this from a tutorial video. So I would check the video again and try to figure out, if you any step/configuration missed, especially if something is done in the scene.

check the tutorial and see how you have to setup the ground object, your groundmask is not registering the collision between the player and the ground

hmm ok ill have a look

Im not sure guys. I really need to get this to work :frowning:

But we can´t do more. There is anything wrong in “Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);” or you didn´t add the script to the player.

Check the objects for groundCheck and groundMask as @raarc already said and check if the position of your player is correct.

Can you post the link of the video maybe we can find something, but it looks like there is something missing/not correct in the inspector

1 Like

Got it working :slight_smile: