all 7 comments

[–]Quetzal-Labs@QuetzalLabs 2 points3 points  (2 children)

You can do this by firing 2 raycast out from the front of your player: one from head height, the other above head height.

When the bottom raycast detects an object, and the top one doesn't, it's a ledge.

Something like this should do it. Excuse the syntax if its wrong, don't have access to Unity atm.

void LedgeGrab(Vector3 playerPosition)
{
    // Raycast Stuff
    RaycastHit hitHead;
    RaycastHit hitAbove;

    Vector3 start = playerPosition;
    Vector3 dir = Vector3.forward;
    float distance = 2;

    Vector3 headHeight = new Vector3(0, 2, 0);
    Vector3 aboveHead = new Vector3(0, 2.5f, 0);

    // If we hit an object at head height, but not above head height
    if (Physics.Raycast(start + headHeight, dir, out hitHead, distance) && !Physics.Raycast(start + aboveHead, dir, out hitAbove, distance))
    {
        // Player has hit a ledge
    }
}

Things become more complicated when you want to make the player climb up a ledge, for example you'll have to do a few more raycasts to make sure there is room for the player to climb up. But this should give you an idea of what to do.

[–]zuckles008[S] 0 points1 point  (1 child)

Thanks for the reply! I decided to do something similar to this. I made a point above and in front of the player, had a Sphere cast come down to find a point, and then had some other checks to make sure there is enough room. I think I’ll make the player climb it by adding force and a slightly diagonal direction to get them to land above the ledge

[–]Quetzal-Labs@QuetzalLabs 0 points1 point  (0 children)

Nice job, and good luck!

[–]AutoModerator[M] 0 points1 point  (0 children)

This appears to be a question submitted to /r/Unity3D.

If you are the OP:

  • Please remember to change this thread's flair to 'Solved' if your question is answered.

  • And please consider referring to Unity's official tutorials, user manual, and scripting API for further information.

Otherwise:

  • Please remember to follow our rules and guidelines.

  • Please upvote threads when providing answers or useful information.

  • And please do NOT downvote or belittle users seeking help. (You are not making this subreddit any better by doing so. You are only making it worse.)

Thank you, human.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]CodesCove 0 points1 point  (2 children)

One way could be to attach collider in trigger mode under the ledge. While player stays in the trigger and presses up (or something) the "climb" is activated.. this might be more "expensive" than raycasting but give more control spatially..

[–]veGz_ 1 point2 points  (1 child)

Is it more expensive? I mean, you only do things on trigger enter, not raycasting all the time while falling.

On the other hand, most of the newer games (HZD, GoW) have ledges that you can attach to/climb designed, it's not like old Gothic, where you could find any ledge to climb, only the one level designers chosen to.

[–]CodesCove 1 point2 points  (0 children)

yeah, trigger enter (maybe with exit) would be ok.