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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Trying to fix the infinite jump of my character but cant... please help!

Discussion in 'Scripting' started by IdleValley, Feb 7, 2021.

  1. IdleValley

    IdleValley

    Joined:
    Nov 30, 2020
    Posts:
    2
    Hello everyone,

    I'm trying to make a rigidbody movement system for my game but I can't seem to fix the infinite jump problem and no fix I'm doing seems to work? Would really love some feedback on the code and hopefully, someone will see where I'm going wrong/if I can improve my code!

    Here is my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7.  
    8. public class MovementController : MonoBehaviour
    9. {
    10.     public float moveSpeed;
    11.     public float jumpForce;
    12.  
    13.     public LayerMask ground;
    14.     public bool isGrounded;
    15.  
    16.     public Rigidbody rigidbody;
    17.  
    18.     void Start()
    19.     {
    20.         rigidbody = GetComponent<Rigidbody>();
    21.     }
    22.  
    23.    
    24.     void Update()
    25.     {
    26.         rigidbody.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, rigidbody.velocity.y, Input.GetAxis("Vertical") * moveSpeed);
    27.  
    28.         if (Input.GetButtonDown("Jump"))
    29.         {
    30.             rigidbody.velocity = new Vector3(rigidbody.velocity.x, jumpForce, rigidbody.velocity.z);
    31.         }
    32.  
    33.        
    34.     }
    35.  
    36.     private bool IsGrounded()
    37.     {
    38.         RaycastHit hit;
    39.         Ray ray = new Ray(transform.position, new Vector3(transform.position.x, -10000, transform.position.z));
    40.  
    41.         if (Physics.Raycast(ray, out hit, 1.6f))
    42.         {
    43.             if (hit.collider != null)
    44.                 return false;
    45.         }
    46.  
    47.         return false;
    48.     }
    49.  
    50.  
    51. }
    52.  
     
  2. GregoryFenn

    GregoryFenn

    Joined:
    Feb 13, 2018
    Posts:
    43
    Forgive me for the dumb answer but... are you actually checking for IsGrounded()?

    Try changing your get-jump-action (line 30) to

    if (IsGrounded() && Input.GetButtonDown("Jump"))
     
  3. IdleValley

    IdleValley

    Joined:
    Nov 30, 2020
    Posts:
    2
    So I tried this
    Code (CSharp):
    1. void Update()
    2.     {
    3.         rigidbody.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, rigidbody.velocity.y, Input.GetAxis("Vertical") * moveSpeed);
    4.  
    5.         if (IsGrounded() && Input.GetButtonDown("Jump"))
    6.         {
    7.             rigidbody.velocity = new Vector3(rigidbody.velocity.x, jumpForce, rigidbody.velocity.z);
    8.            
    9.         }
    But now it's not letting me jump at all D:
    I'm definitely doing a dumb somewhere, I just don't know where!
     
  4. GregoryFenn

    GregoryFenn

    Joined:
    Feb 13, 2018
    Posts:
    43
    What does the
    if (Physics.Raycast(ray, out hit, 1.6f))
    do? Are you actually hitting anything? If not, check that the block of ground has a collider. What are the settings for that collider? Does it cover the object where you think it really does? Is is trigger or not? Also note that if your player is INSIDE the ground (in terms of the colliders) or if your player collider and ground collider overlap, then the ray will not hit the ground. The player's collider should cover almost all its body but hover a tinie-tiny little bit above ground when the scene loads (the game engine will use gravity to rectify that)