Instancing enemy to a position creates issues with my code?

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

When i stance the enemy at Vector2(0,0) the code works fine and the Enemy chases the player but if instance the enemy scene anywhere else for eg. at Vector2(200,200) the enemy goes in a completely different direction

extends KinematicBody2D 
               
export var speed = 350
export var steer_force = 60.0

var velocity = Vector2.ZERO
var acceleration = Vector2.ZERO
onready var target := get_parent().get_node("Player")

				 #VECTOR DIRECTION
func seek():
    var steer = Vector2.ZERO
    if target:
	  var desired_dir = (target.position - position).normalized() * speed
	  steer = (desired_dir - velocity).normalized() * steer_force
    return steer

			   #Enemy MOVEMENT
func _physics_process(delta):
    acceleration += seek()
    velocity += acceleration * delta
    velocity = velocity.clamped(speed)
    position += velocity * delta
 
			  #ROTATION
    rotation = velocity.angle()
     
    if Input.is_action_just_pressed("ui_accept"):
	   var scene_instance = Enemy_Scene.instance()
	   get_tree().current_scene.add_child(scene_instance)
	   scene_instance.position = Vector2(200,200)
	   print (position)

Maybe the problem has something to do with position? Maybe set the global_position of the enemies to Vector2(200, 200)? It could also be the positions of the objects. Maybe change the code for the desired direction to something like this?

var desired_dir = (target.global_position - global_position).normalized() * speed

Ertain | 2021-12-23 20:19

Thanks !
i changed position to global_position and it seems to be working now

javrocks | 2021-12-24 03:48