0 votes

Hi,

I am having a strange issue with the setting of speed for KinematicBody2D, No matter what the value for speedis - the node speed does not change, it is kind of like a constant...

Here is the code:

extends KinematicBody2D

export var speed = 250

onready var screen_size = get_viewport_rect().size
var motion = Vector2()

func _ready():
    set_initial_position()
    set_fixed_process(true)

func set_initial_position():
    randomize()
    var inital_y_position = int(rand_range(160, screen_size.y - 160))
    set_pos(Vector2(0, inital_y_position))

func _fixed_process(delta):
    motion += Vector2(1, 0)
    motion = motion.normalized() * speed * delta
    move(motion)

    if(get_pos().x > screen_size.x + get_node("Sprite").get_texture().get_size().x):
        set_initial_position()

I've noticed that I have the same problem with another KinematicBody2D on the Scene which is a Player both of them are moving with exactly the same speed here the Player code https://github.com/paxer/marine_treasure_hunter/blob/master/scenes/main/player.gd

I also tried instead of using ofmove -

func _fixed_process(delta):
    var fish_position = get_pos()
    fish_position.x += speed * delta
    set_pos(fish_position)

still the same, the speed doesn't change, I instantiate both in a main scene https://github.com/paxer/marine_treasure_hunter/blob/master/scenes/main/main.gd but to me, there is nothing special there what could cause the problem

in Engine by (80 points)

1 Answer

+2 votes
Best answer

export var speed means the variable is exported, which means it can be edited in the scene like other properties of a node. When the scene loads it will set speed to the value set in the scene. If you want to change the value click on the node that has this script and edit the value there. Or remove the export keyword if you only want to edit the value within the script.

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