0 votes

I'm trying to make a 2d top-down Factorio-like game.
I'm having some problems with the player colliding with other objects.
The player uses kinematic-body while rocks use static-bodies. I can't get the player to collide with the rock.

extends Node2D

var Position
const SpeedVert = 3
const SpeedHor = 3

func _ready():

func _process(delta):
    Position = get_pos()
    if Input.is_action_pressed("ui_up"):
        Position.y -= SpeedVert
    if Input.is_action_pressed("ui_down"):
        Position.y += SpeedVert
    if Input.is_action_pressed("ui_left"):
        Position.x -= SpeedHor
    if Input.is_action_pressed("ui_right"):
        Position.x += SpeedHor
    if (not get_node("PlayerBody").test_move(Position)):
        # get_node("PlayerBody").move(Position)

Please help

in Engine by (15 points)

2 Answers

+1 vote
Best answer

Use move_to( Position ), not set_pos(), no need to call test_move().

In Godot 3 you want to call move_and_collide() but it needs vector relative to player's position

var relativeVector = Vector2(0, 0)

put it in _process() and modify according to user input. You won't need Position variable.

BTW do you have CollisionShape2D nodes in your body nodes?

by (2,298 points)
selected by
+1 vote

Are you using Godot 3.0? Maybe you want to use moveandcollide()?

See the docs: http://docs.godotengine.org/en/latest/classes/class_kinematicbody2d.html

by (192 points)
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.