+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

The node structure is
---- 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():

func change_state(new_state):
    state = new_state
    match state:
            new_anim = 'idle'
            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:
        velocity.x += run_speed
    if left:
        velocity.x -= run_speed
    if up:
        velocity.y -= run_speed
    if down:
        velocity.y += run_speed

    $AnimatedSprite.flip_h = velocity.x < 0
    if !right and !left and !up and !down and state == RUN:

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):
    if new_anim != anim:
        anim = new_anim

func _physics_process(delta):
in Engine by (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

by (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.
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 webmaster@godotengine.org with your username.