How do I find the angle of a collision?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LeipeMeuteLeider

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?

:bust_in_silhouette: Reply From: Gluon

You can use a raycast2d for this. Documentation here

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.