+1 vote

I am just starting to play around with Godot and I have run into an issue when making use of the KinematicBody2D node and moving it using the move and collide method. The sprite seems to be moving at a different pace to the global_position and position methods.

This is a video of the issue. The red dot is drawn at the global_position and the white dot is drawn at the position
https://drive.google.com/open?id=1kYpV4h0Dj4sWCtl38KKguzdKNfnyA3Bk

The node structure is
KinematicBody2D
---- AnimatedSprite
---- CollisionShape2D
The code is below it's pretty basic any help would be appreciated.

extends KinematicBody2D

export (int) var run_speed

enum {IDLE, RUN}
var velocity = Vector2()
var state
var anim
var new_anim
var bullet = preload("res://Bullet.tscn") 

func _ready():
    change_state(IDLE)

func change_state(new_state):
    state = new_state
    match state:
        IDLE:
            new_anim = 'idle'
        RUN:
            new_anim = 'run'

func get_input():
    velocity = Vector2()
    var right = Input.is_action_pressed('right')
    var left = Input.is_action_pressed('left')
    var up = Input.is_action_pressed('up')
    var down = Input.is_action_pressed('down')

    if right:
        change_state(RUN)
        velocity.x += run_speed
    if left:
        change_state(RUN)
        velocity.x -= run_speed
    if up:
        velocity.y -= run_speed
        change_state(RUN)
    if down:
        velocity.y += run_speed
        change_state(RUN)

    $AnimatedSprite.flip_h = velocity.x < 0
    velocity.normalized()
    if !right and !left and !up and !down and state == RUN:
        change_state(IDLE)

func _draw():
    draw_circle(position, 10, Color(1,1,1,1))
    draw_circle(global_position, 5, Color(0.86, 0.08, 0.24, 1))


func _process(delta):
    update()
    get_input()
    if new_anim != anim:
        anim = new_anim
        $AnimatedSprite.play(anim)

func _physics_process(delta):
    move_and_collide(velocity*delta)
asked Dec 5, 2019 in Engine by jdumpleton (22 points)

1 Answer

+1 vote
Best answer

I have solved this.

The draw circle method creates and appends a node to the creating node as such the position supplied when created needs to be defined as an offset of the parent node. For example to create a circle on the center of the parent sprite the supplied position would be 0,0

answered Dec 5, 2019 by jdumpleton (22 points)

I just spent 20 minutes trying to solve this, thanks for indirectly solving my issue!

This isn't made clear in the documentation. You should create an issue.

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 How to use this Q&A? before posting your first questions.