![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | jdumpleton |
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)