Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Jump not working need help

Discussion in 'General Discussion' started by unity_18C29E37BD626C0361FD, Apr 11, 2021.

  1. unity_18C29E37BD626C0361FD

    unity_18C29E37BD626C0361FD

    Joined:
    Apr 11, 2021
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovementScript : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.  
    9.     public float speed = 12f;
    10.     public float gravity = -9.81f;
    11.     public float jumpHieght = 3f;
    12.  
    13.     public Transform GroundCheck;
    14.     public float groundDistance = 0.4f;
    15.     public LayerMask groundMask;
    16.  
    17.     Vector3 velocity;
    18.     bool isGrounded;
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         isGrounded = Physics.CheckSphere(GroundCheck.position, groundDistance, groundMask);
    24.  
    25.         if(isGrounded && velocity.y < 0)
    26.         {
    27.             velocity.y = -2f;
    28.         }
    29.  
    30.         float x = Input.GetAxis("Horizontal");
    31.         float z = Input.GetAxis("Vertical");
    32.  
    33.         Vector3 move = transform.right * x + transform.forward * z;
    34.  
    35.         controller.Move(move * speed * Time.deltaTime);
    36.  
    37.         if (Input.GetButtonDown("Jump") && isGrounded)
    38.         {
    39.             velocity.y = Mathf.Sqrt(jumpHieght * -2f * gravity);
    40.         }
    41.  
    42.         velocity.y += gravity * Time.deltaTime;
    43.  
    44.         controller.Move(velocity * Time.deltaTime);
    45.     }
    46. }
    47.  
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    There's lots of threads on reliability issues of the built in isGrounded. I'd guess that is the reason. It would help though if the OP described their problem with more than "not working" and a script dump.