Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

My jump button only works sometimes

Whenever I press the jump button it will only sometimes make me jump. I have to spam it to get it to do what it want. Any idea on how to fix it?

Here's my code for movement from the Brackeys video:

using UnityEngine;


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

   


   

    // Start is called before the first frame update

    void Start()

    {

        

    }


    // Update is called once per frame

    void FixedUpdate ()

    {

//Velocity cancelling

       isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

       

       if(isGrounded && velocity.y < 0)

       {

           velocity.y = -2f;

       }

//Player controls

       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);

       }


        //Gravity controls

       velocity.y += gravity * Time.deltaTime;


       controller.Move(velocity * Time.deltaTime);

    }


}

Sign In or Register to comment.