Make player bounce off world

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

Hello!

im making a top down game.

the player is a kinematic body 2d that auto moves continually forward, you just rotate the direction. this works perfect, but i want that the player bounces forward (as in the image attached) when it collides with the tilemap world

this is the player movement code:

extends KinematicBody2D
    
var rotation_speed = 3
var rotation_dir = 0
var direction = 1
var speed = 50
var velocity = Vector2()
    
func _physics_process(delta):

	rotation += rotation_dir * rotation_speed * delta
	velocity = Vector2(speed, 0).rotated(rotation) * direction
    velocity = move_and_slide(velocity)

i’ve tried changing the last line with move_and_collide (times delta) but it doestn work. on a collision with the world, the player just gets stuck.

thank you very much!

:bust_in_silhouette: Reply From: spaceyjase

Here you go: Using CharacterBody2D/3D — Godot Engine (stable) documentation in English