+1 vote

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 moveandslide.
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 :(

``````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
``````
in Engine
edited

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

Hi,
are you sure the getslidecollision(0) is the wall? ... and not the ground?

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

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.

and are you sure that velocity is > 500?

i would

``````if velocity.length() > 500:
if get_slide_count() > 0:
breakpoint
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