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

Question Rigidbody Raycast doesn't detect ground

Discussion in 'Scripting' started by GraySkayl, Jun 14, 2023.

  1. GraySkayl

    GraySkayl

    Joined:
    Jun 11, 2023
    Posts:
    4
    Hi. I'm sort of new, and I'm trying to make a basic 3d first person controller system. I have the WASD movement down pretty well but I'm having trouble jumping. I have tried many different methods, but I can't seem to get it to know when it's touching the ground. I've tried raycasts like many people suggest, but if I log it into the console it constantly says that the player is not grounded. Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Jump : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.     public float jump = 5f;
    9.     private bool isGrounded;
    10.  
    11.     private void Update()
    12.     {
    13.         J();
    14.     }
    15.  
    16.     void J()
    17.     {
    18.         if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    19.         {
    20.             rb.AddForce(Vector3.up * jump, ForceMode.Impulse);
    21.         }
    22.     }
    23.  
    24.     void GroundCheck()
    25.     {
    26.         RaycastHit hit;
    27.         float distance = 1f;
    28.         Vector3 dir = new Vector3(0, -1);
    29.  
    30.         if (Physics.Raycast(transform.position, dir, out hit, distance))
    31.         {
    32.             isGrounded = true;
    33.         }
    34.         else
    35.         {
    36.             isGrounded = false;
    37.         }
    38.     }
    39.  
    40.  
    41.  
    42. }
    This code is sort of Frankensteined from many different sources as I was trying to figure out how to get something to work, but nothing seemed to work. Can anyone help me get this to work? Also, this code may be a bit sloppy, so on a side note, please help clean this up a bit.
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    483
    You're never calling your
    GroundCheck
    method, so it isn't even doing any sort of checks for whether the player is grounded or not.

    Perhaps you should actually call
    GroundCheck()
    before you call
    J()
    (which is a terrible name for a method TBH) so that it sets the
    isGrounded
    variable.

    Without calling the method the variable is never going to be set.
     
    GraySkayl likes this.
  3. GraySkayl

    GraySkayl

    Joined:
    Jun 11, 2023
    Posts:
    4
    This is a super obvious one lol, thanks. This works. However, it seems the player only jumps most of the time I hit space instead of all of the time. Do you know why this may be?
    Edit: I also changed the name of the jump function. lol