How to make a player stop when colliding

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RobinGerst99
:warning: Old Version Published before Godot 3 was released.

So basically, I have a square like player made from a kinematicbody2d, a sprite and a collisionshape2d(square). I also have a maze of “walls” setup. Yet, when I navigate my player against a wall he seems to move into it until about half way. Next, he vigorously jumps back. Doing this repetitively makes him look as though he is teleporting back and forth. All I want is for him to stop moving once he is touching one of the walls. Any clues? I pasted the code below if that helps.

extends KinematicBody2D
var velocity=Vector2(0,0)
var WALK_SPEED=25

func _ready():
set_process(true)
self.get_child(0).set_scale(Vector2(0.05,0.05))
self.get_child(1).set_scale(Vector2(0.05,0.05))
set_pos(Vector2(31,32))
pass

func _process(delta):
if (Input.is_action_pressed(“ui_left”)):
velocity.x = - WALK_SPEED
velocity.y=0
elif (Input.is_action_pressed(“ui_right”)):
velocity.x = WALK_SPEED
velocity.y=0
elif (Input.is_action_pressed(“ui_up”)):
velocity.y = -WALK_SPEED
velocity.x=0
elif (Input.is_action_pressed(“ui_down”)):
velocity.y = WALK_SPEED
velocity.x=0
else:
velocity.x = 0
velocity.y=0

var motion = velocity * delta
motion = move(motion)
pass

You should fix your code formatting. You can either indent four spaces:

like this

Or use the “{}” button in the formatting menu above.

As it stands, it’s impossible to tell if your code is indented properly.

kidscancode | 2017-09-11 21:42

As far as I can see, your code only moves a body using standard 4-way directions and move, but you are using _process instead of _fixed_process.
Also, did you have a look at the official introduction to KinematicBody2D? Kinematic character (2D) — Godot Engine (stable) documentation in English

Zylann | 2017-09-12 12:36