Is there any way to make a kinematic body 2D bounce on wall using move_and_slide?

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

I’m trying to make kind of a smash bros like fighting game but simplier and everything it’s working the way that I imagine, except the knockback hit.
I want that when the character knockback with high speed, he bounces on wall.
I’m using a Kinematic Body 2D with the move_and_slide.
I’ve tried to get the collision and multiply the velocity by the collision normal but it just don’t make anything.

if velocity.length() > 500:
    if get_slide_count() > 0:
        velocity = velocity * get_slide_collision(0).normal

I’ve also tried to use the .bounce() but also dont work :frowning:

if velocity.length() > 500:
    if get_slide_count() > 0:
        velocity = velocity.bounce(get_slide_collision(0).normal)

When I use a print to check, everything is working, it gets the collision and the normal, it multiplies, it just dont make anything.
I completely sure that I’m making something wrong.

Here’s a Preview (the character is the blue one):

Edit: Someone on Reddit have helped me.
It happens that the velocity that I was getting, it’was when the character stop, so i just needed to make a variable with the previous velocity:

var prev_velocity = velocity 

velocity = move_and_slide(velocity, UP)
	if prev_velocity.length() > 500:
		if get_slide_count() > 0:
			velocity = prev_velocity.bounce(get_slide_collision(0).normal) * 0.8

Uh, there is no visible preview on your question. :slight_smile:

Millard | 2020-09-17 04:23

are you sure the get_slide_collision(0) is the wall? … and not the ground?

klaas | 2020-09-17 10:59

Sorry, don’t know why the image isn’t working but i have edited with the direct link.

Eduuuuu | 2020-09-17 11:53

Yeah, but I want to bounce in any direction so it’s not gonna be a problem if the character bounces on the ground too. I just kinda confused with the math and what method should i use.

Eduuuuu | 2020-09-17 12:02

and are you sure that velocity is > 500?

i would

if velocity.length() > 500:
    if get_slide_count() > 0:
        var collision = get_slide_collision(0)
        velocity = velocity.bounce(collision.normal)

and step into it … looking at the values of collision and the velocity result in the debugger

klaas | 2020-09-17 12:08