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

I need help with the infinite jumping

Discussion in 'Scripting' started by afs3122, Dec 17, 2020.

  1. afs3122

    afs3122

    Joined:
    Dec 9, 2020
    Posts:
    13
    I'm new to programming and unity and I've encountered the problem where I can jump forever please help


    Script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    public CharacterController controller;
    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    Vector3 velocity;
    bool isGrounded;

    public Transform groundCheck;
    public float groundDistnace = 0.4f;
    public LayerMask groundMask;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {



    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistnace, 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);
    }





    }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Use code tags.

    Check your console for error messages.

    My guess is that there's something wrong with your ground check. Possibly you have set one of the parameters incorrectly, such as using a ground check position that doesn't move with the player, or setting a ground mask that matches the sky.
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    if you can always jump, then isGrounded must always be true. So your code here is not working somehow:
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistnace, groundMask);


    Are you sure that the groundMask is excluding the player, for example?
     
  4. afs3122

    afs3122

    Joined:
    Dec 9, 2020
    Posts:
    13
    Well... when I change the player's layer to Default and the ground's layer is ground I put a debug log and it just spams isGrounded: Falsethere fore I cant jump but when the player is layerd as ground it lets me jump but infinitely
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    OK, so you have successfully identified a problem where your grounded check was returning true for an incorrect reason, and a second problem where it fails to return true for the correct reason.

    That means you need to fix both of those problems.

    You've already determined you can fix the first problem by not putting the player in the ground layer.

    To figure out how to fix the second problem will require further investigation, but it will probably be related to at least one of the three ground check parameters (position, distance, and/or mask) being set incorrectly. That is, probably you are looking for ground in the wrong place, or you are looking in too small an area, or you are failing to recognize it when you find it. (That third possibility is unlikely, since classifying the player as ground caused you to find ground.)
     
  6. afs3122

    afs3122

    Joined:
    Dec 9, 2020
    Posts:
    13
    The only thing is that I just got it from a tutorial
    so I don't know what to change, everybit of code I got from the tutorial
     
  7. afs3122

    afs3122

    Joined:
    Dec 9, 2020
    Posts:
    13
    omg i just figured it out I didn't do anything wrong with the code it was just in the inspector upload_2020-12-17_15-17-53.png
    I put the Ground check to the 1st person player which wasn't touching the ground