Godot Version
v4.5.1 stable
Question
Hi! I’m working on a top-down 3D game and I’m having some trouble to try and find out the best way to prevent my player character from walking off a ledge.
I tried using a raycast in front of the player at first, and if the raycast isn’t hitting anything stop the player from moving, but it posed some issues. If the player jumps right on the ledge or tries walking along the ledge the raycast won’t be hitting, preventing movement, but the player collision is still big enough for the player to keep standing on the ledge, which feels really janky.
Basically, I need the player to be able to easily walk up to and along a ledge but not off of it.
Any possible solutions?
Edit: I should add that I don’t want to use invisible walls or such as I want the player to be able to jump off a ledge, just not walk off.
Well prevent only the movement in the direction towards the raycast if it’s not hitting, allow movement in other directions.
1 Like
While that sounds reasonable enough, it also sounds way easier on paper than actually to implement. The player has full 360 degree movement, how exactly would I go about getting the direction of the movement relative to the raycast and block only in that direction? 
Sorry if I sound stupid, I’m just not that experienced in coding.
For a more specific suggestion, perhaps describe precisely what constitutes a “ledge” in your game and how the control scheme works. Maybe post some screenshots/videos as well.
In general, you can always just put invisible colliders along the ledges, blocking the player to walk off.
Ledge in this case means any ending of the floor the player could fall off of.
As an example: in this video player can move towards the edge and jump along it just fine until the raycast stops hitting, but jumping or walking diagonally towards the end of the floor where the raycast isn’t hitting but the player is still standing on the floor ends up with the player not being able to move along the ledge. Sorry if I’m not describing it well enough, don’t know any better way to tell it.
As I said above - use colliders along the ledges.
I was hoping to do a prevention system fully in code, so that I don’t have to put invisible walls anywhere. And also, in that case I would most likely have to make the colliders very short so that the player can still jump over the ledge, as I want the player to be able to jump off of a platform but not walk off of it, it’s just a bit tedious if you know what I mean.
I worked with unreal previously and that engine has a built in option to stop player from walking off ledges, I was hoping a solution like that would be somewhat simple to implement, but it doesn’t seem so.
For jumping, disable the colliders as soon as the jump action is pressed and enable them back when the player lands.
It could also be done with a couple of raycast that are constantly in front of the player. Not complicated, just a bit of vector math.
I’m definitely going to keep invisible colliders in mind if I can’t think of anything else, but for now I’ll try to find a way around this and hopefully get it figured out with the raycasts.
What’s player’s collision shape and how far you want to allow it to go beyond the edge?
The collision is a cylinder. I want it to be able to go slightly off the ledge, just enough to let the player walk across the ledge comfortably but so that the player model doesn’t visibly float.
And how it the movement direction determined? Analog stick?
Analog stick and/or combination of WASD keys. As I said player can move around 360 degrees.
What happens if the player walks into the ledge at some angle. If you want them to slide along the ledge as is the case with walls then the easiest solutions is to use colliders. Implementing sliding with just raycasts won’t really be possible as raycasts cannot know what the direction of the edge is.
Walking towards the ledge at an angle results in the same behavior, it stops the player from moving as soon as the raycast stops colliding.
If the angle is very sharp it still may be perceived as if player cannot walk along the ledges.
Yeah, I want the player to be able to walk along the ledge freely just enough so that they don’t fall, and always stop if they might fall basically.
Then I don’t really see any problem doing it with raycasts. Use two of them in front of the player separated a bit along sideways axis. If both don’t collide - stop moving.
This works fine until a point, just like previously with a single raycast, if the player keeps trying to walk into the ledge at an angle by going up/down and then right quickly, at one point they wiggle into the ledge just enough so that both raycasts aren’t colliding, making the player unable to move along the ledge any further. I’m not sure if this is caused by the nature of my player movement system, as there are systems like acceleration and deceleration present, but it results in the behavior that I presented earlier, where you can no longer move along the ledge anymore.
Are you forcing the raycast update?