Instanced object doesn't react or move as expected

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DevAwk

I am curently following this 3.5 tutorial on a Strategy Game in Godot ( and I am having trouble translating this to Godot 4.
I’m currently on the first part of the tutorial, and for some reason whenever I want to send out a “soldier” it spwans a sprite at the center of my camera and doesn’t move (even though it should be moving as I have added a _physics_process to my soldier scene to have it move automatically).

Relevant code for my castle scene:

var soldiers_node = preload("res://Scenes/soldiers.tscn")

func send_soldiers(target):
 if soldiers > 1:
	var instance = soldiers_node.instantiate()
	instance.position = position
	instance.send_out(target, soldier_speed, floor(soldiers / 2), player_owned)
	soldiers -= floor(soldiers / 2)

Relevant code for my soldier scene:

func _physics_process(delta):
  if target != null:
	velocity = (target.position - position).normalized() * speed
	position += velocity * delta

I can provide more context if needed, but I’ve been on this problem for a day now and I’m stumped… Is it a translation problem from Godot 3.5 to 4, or am I missing something crucial?

:bust_in_silhouette: Reply From: deaton64

I started to watch the video, but I just couldn’t, it was a bit slow for me.

Try this:

func _physics_process(delta):
  if target != null:
    velocity = (target.position - position).normalized() * speed
    position += velocity * delta

and see what target is set to… is it null?

If it’s not null, try this:

func _physics_process(delta):
  if target != null:
    velocity = (target.position - position).normalized() * speed
    position += velocity * delta

is velocity 0?

if both of the above have some output, try this:

func _physics_process(delta):
  position += 1

does the solider move?

If it doesn’t move, can you upload your project somewhere so someone can take a look?

After writing some print statements (I realize I probably should have done that first before coming here…), I realized that my code was inputting the origin castle as the target, rather than the actual castle.

Sorry ;-;

DevAwk | 2023-04-06 01:48

great, glad you sorted it.

deaton64 | 2023-04-06 15:21