The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

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 (18 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 (3,328 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 Frequently asked questions and 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 [email protected] with your username.