0 votes

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

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)

enter image description here

What is wrong?

in Engine by (61 points)

1 Answer

+2 votes
Best answer

When using the KinematicBody2D node avoid using set_pos to change position. Use move instead. The move method was created with collisions in mind, when the object collides move won't allow the object to go any further.

by (699 points)
selected by

Thank you very much. Now it's working. :)

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 [email protected] with your username.