Godot 4 velocity.bouce method stops scene on wall

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

Hello, I am trying to get my enemy scene to bounce off a wall. It’s using the CharacterBody2D and here is my _physics_process method:

func _physics_process(delta: float):
    var input_direction = Vector2(1, 1)
    velocity = input_direction.normalized() * move_speed


    var collision: KinematicCollision2D = move_and_collide(velocity * delta)
    if collision:
	    velocity = velocity.bounce(collision.get_normal())

However, when the above runs the character just sticks to the wall that it collided with and doesn’t move. I tried calling move_and_slide() in that if statement but the character just jitters and moves straight down.

I have looked up tutorials and other posts but those are from Godot 3.X. One of the changes that I noticed is that collision.normal is now collision.get_normal() I believe.

Any help on this would be great!

:bust_in_silhouette: Reply From: zachstarnes

I was able to fix it by adding an if statement around the original set velocity at the top of the method:

if velocity == Vector2.ZERO:
	velocity = input_direction.normalized() * move_speed