+2 votes

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 by (16 points)
edited by

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

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:
        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

Please log in or register to answer this question.

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 webmaster@godotengine.org with your username.