How can I improve my Ground Check?

using System;
using System.Threading;
using UnityEngine;
//using UnityEngine.UIElements;
using UnityEngine.XR;

public class Player_move : MonoBehaviour
{
    //movement variables
    public CharacterController characterController;
    public float MovementSpeed = 7.75f;

    //gravity && jump variables
    public float Gravity = -0.25f;
    public Vector3 velocity;
    public float jump_height = 0.03f;
    //ground check variabels
    public Transform GroundCheckC;
    public float groundDistance = 0.1f;
    public float jumpdistance = 0.5f;
    public LayerMask groundMask;
    bool isGrounded;
    bool canjump;

    //rotation variables
    private float camera_x_rotation = 0;
    private float camera_y_rotation = 0;
   

    private void Start()
    {
        characterController = GetComponent<CharacterController>();
       
       
    }

    void Update()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
        isGrounded = Physics.CheckSphere(GroundCheckC.position, groundDistance, groundMask);
        //Physics.Check
        canjump = Physics.CheckSphere(GroundCheckC.position, jumpdistance, groundMask);
        //player movement - forward, backward, left, right

        float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;
        float vertical = Input.GetAxis("Vertical") * MovementSpeed;
        Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= MovementSpeed;
        characterController.Move(moveDirection * Time.deltaTime);
        //camera rotation
        float rotation_speed = 4;
        camera_x_rotation += Input.GetAxis("Mouse Y");
        camera_y_rotation += Input.GetAxis("Mouse X");
        float max_x_rotation = -70;
        camera_x_rotation = Mathf.Clamp(camera_x_rotation, max_x_rotation, -max_x_rotation);
        transform.localRotation = Quaternion.AngleAxis(camera_y_rotation, Vector3.up);
        Camera.main.transform.localRotation = Quaternion.AngleAxis(-camera_x_rotation, Vector3.right);
       


        //Gravity && Jump
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = 0f;
        }
        if (Input.GetButton("Jump") && canjump)
        {
            velocity.y = Mathf.Sqrt(jump_height * -2f * Gravity);
        }

        velocity.y += Gravity * Time.deltaTime;

        characterController.Move(velocity);



    }
    }

Currently When I’m on a ledge, it wont think I’m Grounded, and this this happens:

https://i.gyazo.com/f36568f1c85d1a35686637c8b2738657.gif

I can’t just make the groundDistance larger, because otherwise im very far off the ground, and it messes up other puzzles and such.
So how can i change this, to avoid the problem, and still be close to the ground.
PS It takes about a groundDistance of .5 in order to avoid this, but then im 5x higher in the air which is very far.

The above code looks originally derived from the Unity API scripting reference for CharacterController, which used to work fine.

HOWEVER… today I just saw a second post (this post is a third post) reporting issues with ground detection.

I wrote it up over here:

Along with a patched script to fix the problem.

The main fixes I made are:

  • to only call .Move() once per frame, with a fully-fleshed out velocity instead of twice the way you are above (and the way the Unity sample shows)

  • to let “on ground” linger momentarily after you are not on the ground, to facilitate jumping even when you are “stumbling” down a ramp, which the original code failed on.

Take a look, shouldn’t be too hard to fix the above code the same way, at least if that is the problem.

1 Like