This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

So, I've been working on a stategy game and I ran into a problem: my kinematic body Soldier is instanced, when I init the instance in _ready() function like this:

const Soldier = preload("res://Scenes/Soldier.tscn")
   var soldier = Soldier.instance()

it works perfectly fine and moves as it should.
Here's a movement script btw:

if Input.is_action_just_pressed("click"):
    firstMove = true #make sure that correct animation is playing
    target = get_global_mouse_position() #script here is combination of my work and Godot docs page
    trans = Vector2(target.x - position.x, target.y - position.y).normalized() * speed
    rotation = trans.angle() + deg2rad(90)

if (target - position).length() > 5 && firstMove == true:
    legs.animation = "walk"
    legs.animation = "idle"

I also have a script that duplicates soldier. It's attached to the root node:

if Input.is_action_pressed("duplicate"): #that means pressing D
    var soldier = Soldier.instance()

The thing is the initial soldier walks as it should, but duplicated teleport around like crazy. Here's the video on Imgur

in Engine by (32 points)

Coding Tips: Instead of writing

trans = Vector2(target.x - position.x, target.y - position.y).normalized() * speed


var to_target = target - position
trans = to_target.normalized() * speed

then in your if statement you can use it again:

if to_target.length() > 5 && firstMove:

Don't write firstMove == true just firstMove or not firstMove for firstMove == false.

1 Answer

+1 vote
Best answer

Likely it's because you're creating a new soldier every frame you're pressing duplicate with Input.is_action_pressed. This causes those newly created soldiers to collide and "teleport" like that.

Solution: Use Input.is_action_just_pressed:

if Input.is_action_just_pressed("duplicate"):
    var soldier = Soldier.instance()

This will only create a new soldier only once every you press duplicate instead of every frame.

by (4,246 points)
selected by

Thank you very much!!!

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.