I am learning Godot game engine and I tried to make a ball that can walk and can't go through other objects, but the ball keep going through the other objects.
Here is the code:
extends KinematicBody2D
export var viteza = 140
func _ready():
set_fixed_process(true)
func _fixed_process(delta):
delta *= 5
var motion = Vector2()
if Input.is_key_pressed(KEY_UP):
motion[0] = 0
motion[1] = -1
if Input.is_key_pressed(KEY_DOWN):
motion[0] = 0
motion[1] = 1
if Input.is_key_pressed(KEY_LEFT):
motion[0] = -1
motion[1] = 0
if Input.is_key_pressed(KEY_RIGHT):
motion[0] = 1
motion[1] = 0
motion = motion * viteza * delta
set_pos(get_pos() + motion)
The scene (image): Scene
What is wrong?
Never use set_pos() in a KinematicBody2D. You need to use move().