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

Help please

Discussion in 'Getting Started' started by SHAUZZ, Sep 29, 2020.

  1. SHAUZZ

    SHAUZZ

    Joined:
    Jul 27, 2020
    Posts:
    8
    Help my character is jumping infinitly what to do I found the code on youtube brackeys channel im super new to programming.
    Here is the code

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

    public class PlayerMovement : MonoBehaviour
    {
    public CharacterController controller;

    public float speed = 10f;
    public float gravity = -10f;
    public float jumpHeight = 5f;

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

    Vector3 velocity;
    bool isGrounded;

    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.GetButton("Jump") && isGrounded){
    velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);

    }
    }
     
  2. UnityMaru

    UnityMaru

    Community Engagement Manager Unity Technologies

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    Hey there,

    Could you link the video/tutorial? Also, would be handy to understand what version of the Editor you are using :)
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Use Debug.Log. Check what the value of isGrounded is. Check if the "Jump" button is being pressed when you expect it not to be pressed. Output the value of velocity.y. "gravity" is a public field, so make sure it is set to a negative number, such as -10f, in the inspector. A positive gravity value would result in just flying away whenever not jumping. etc, etc, etc good luck
     
  4. SHAUZZ

    SHAUZZ

    Joined:
    Jul 27, 2020
    Posts:
    8
    UnityMaru this
     
  5. SHAUZZ

    SHAUZZ

    Joined:
    Jul 27, 2020
    Posts:
    8
    Im using 2019.4.11
     
  6. SHAUZZ

    SHAUZZ

    Joined:
    Jul 27, 2020
    Posts:
    8
    Joe Censored how do you check the value of IsGrounded im very new to unity
     
  7. SHAUZZ

    SHAUZZ

    Joined:
    Jul 27, 2020
    Posts:
    8
    I can jump while in the air what to do
     
  8. UnityMaru

    UnityMaru

    Community Engagement Manager Unity Technologies

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    Judging by the YouTube comments, a lot of users are running into the same problem. You may want to look through those comments for a solution.