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 Player gets stuck on the edge of an object rather than falling?

Discussion in 'Scripting' started by ResonantWilterUni, Sep 13, 2023.

  1. ResonantWilterUni

    ResonantWilterUni

    Joined:
    Jul 20, 2023
    Posts:
    41
    When I either jump onto an object or slowly walk off it like in the gif, the entire bottom of my capsule is clearly not grounded so my player should fall down. I understand that the side of the capsule is touching the cube object, which is why in my code below I have tried multiple variations of the
    isGrounded
    check. Also for majority of the gif the
    isGrounded
    bool is false yet my player still does not try to fall down.



    For testing purposes I am using Brackey's first person character controller movement code since it's very simple and I can make sure that there is no overlapping code, etc, but whatever I change, the end result is the same. I have the groundCheck object at the bottom of the capsule and I have tried changing both the origin and radius of the SphereCast so that it's not hitting the cube object, but that hasn't helped and even the Raycast is having the same issue. Decreasing the Skin Width in the inspector for the controller also has not changed anything.

    Code (CSharp):
    1. void Update()
    2. {
    3.     isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    4.     //isGrounded = controller.isGrounded;
    5.     //isGrounded = Physics.SphereCast(transform.position + transform.up * -0.5f, 0.25f, Vector3.down, out hit, 1f)
    6.     //isGrounded = Physics.Raycast(transform.position, -transform.up, 2f);
    7.  
    8.     if (isGrounded && velocity.y < 0)
    9.     {
    10.         velocity.y = -2f;
    11.     }
    12.  
    13.     float x = Input.GetAxis("Horizontal");
    14.     float z = Input.GetAxis("Vertical");
    15.  
    16.     Vector3 move = transform.right * x + transform.forward * z;
    17.  
    18.     controller.Move(move * speed * Time.deltaTime);
    19.  
    20.     velocity.y += gravity * Time.deltaTime;
    21.     controller.Move(velocity * Time.deltaTime);
    22. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Looking at this:

    Screenshot 2023-09-13 at 11.14.21 AM.png

    ... you will want to abandon this script's approach. Here is why:

    I wrote about this before: the Unity example code in the API no longer jumps reliably.

    If you call .Move() twice in one single frame, the grounded check may fail.

    I reported it to Unity via their docs feedback in October 2020. Apparently it is still broken:

    https://docs.unity3d.com/ScriptReference/CharacterController.Move.html

    Here is a work-around:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746

    I recommend you also go to that same documentation page and ALSO report that the code is broken.

    When you report it, you are welcome to link the above workaround. One day the docs might get fixed.

    If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!
     
  3. ResonantWilterUni

    ResonantWilterUni

    Joined:
    Jul 20, 2023
    Posts:
    41
    Thanks for the thread link. I took a look at it and your code and I used it in my project but the problem still persisted. I also created a brand new empty project and copied and pasted the work-around code but I was still getting the same issue as what I have in my gif above.

    I genuinely have no idea what is wrong. I've put in the time to learn more about collision & the isGrounded check and have gone through many potential solutions but yeah, no luck sadly.
     
  4. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,104
    Have you checked that determining the value of
    isGrounded
    is the actual problem?

    If the character is on top of a flat surface, and you just apply downward force to the character, its collider will just get pressed against the surface, and won't magically fall through the ground or move laterally over the edge.

    You can use a capsule shaped collider to have the character naturally start falling over an edge due to gravity once most of the character's body is over it.

    You can also use multiple raycasts to determine when a large part of the character's body is over a ledge, and then apply lateral force in the direction of the ledge to push the character over it.
     
  5. ResonantWilterUni

    ResonantWilterUni

    Joined:
    Jul 20, 2023
    Posts:
    41
    Yeah, I think that there might be more to it than I originally thought. When I tested it further, while my character is stuck on top of a flat surface, the
    isGrounded
    bool is false during it. So I assumed that because my character is not grounded, that the game should automatically "push" my player backwards and then gravity would push my player downwards to the ground.

    Also, my character controller has no colliders and I'm currently only using the in-built character controller component. I have tried more solutions since creating my thread but I'm stumped.
     
    SisusCo likes this.
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    Why would your character be "pushed backward"?
     
  7. ResonantWilterUni

    ResonantWilterUni

    Joined:
    Jul 20, 2023
    Posts:
    41
    My bad I didn't explain it properly.

    Like for example when the player is trying to jump onto an object that is too high, so the player "slips" backwards off the edge of the object by default and then continues to fall down like normal.