0 votes

I've just made some basic movement for my 2d game, but I'm having an issue. If my character collides with something on the screen, the character stops moving, but the character's velocity in code still remains, and can even be raised(for example gravity constantly compounding whilst you are standing still, leading to getting teleported off a platform down to the next because velocity has grown as if you are still falling whilst standing on the higher platform, or being stuck to a wall for a few seconds because it takes that long to cancel your velocity towards the wall). Obviously this isn't supposed to happen, but I can't figure out how to stop the character from moving in only the direction where the character collided with something(I can stop the player but then any sideways velocity when landing on something is also cancelled). All I could find on the matter are threads from previous versions of godot with outdated methods that don't seem to work anymore. How would I go about this nowadays?

in Engine by (16 points)

1 Answer

+1 vote

You can use a raycast2d for this. Documentation here

https://docs.godotengine.org/en/stable/classes/class_raycast2d.html

what you can do for example with your point about a platform under the character you can have a raycast looking "down" and if the raycast detects a platform you use a signal to indicate that. The signal can then control your code for the velocity for example you could have a boolean "onGround" and set it as true. When your character is on the ground have the raycast change this to true, when the raycast doesnt detect ground set it to false. Then you can simply only run the velocity code when the boolean is true.

by (1,635 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.