Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why can i jump forever

Discussion in 'Scripting' started by Goosbag, Jun 8, 2022.

  1. Goosbag

    Goosbag

    Joined:
    Jun 5, 2022
    Posts:
    2
    Hi, i have a problem with my code it makes it so if you hold down jump you just go up can anyone help?
    (this is my code)
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovment : 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"))
    {
    velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);

    }
    }
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    You should only be able to Jump when you are grounded, I guess.

    Code (CSharp):
    1. if(isGrounded && Input.GetButtonDown("Jump"))
    2. {
    3. velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    4. }
    And you should use Code-Tags ;-)
     
    Bunny83 and TheDevloper like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,757
    The reason you can jump forever is likely related to this code coming from defective Unity example code. I surmise this because the .Move() method is called twice in one frame, which is a no-no.

    I've written about this so many times but this time I'm going to tag @MelvMay so perhaps the bad documentation might eventually get fixed. See link below.

    I wrote about this before: the Unity example code in the API no longer jumps reliably.

    I reported it to Unity via their docs feedback in October 2020. Apparently it is still broken:

    https://docs.unity3d.com/ScriptReference/CharacterController.Move.html

    Here is a work-around:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746

    I recommend you also go to that same documentation page and ALSO report that the code is broken.

    When you report it, you are welcome to link the above workaround. One day the docs might get fixed.

    If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    Unfortunately that's 3D physics so not related to me: Someone such as @yant could comment.
     
    Kurt-Dekker likes this.
  5. Goosbag

    Goosbag

    Joined:
    Jun 5, 2022
    Posts:
    2
    Thank you for the help
     
  6. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    this is the best solution, checking the player if he is grounded + checking the jump button.