Why are my enemys moving down

i just want it to follow me and attack me man :frowning:

Tough to see your scripts. maybe use global_position for your direction to calculation

@gertkeno sorry that the video was bad ill paste my scripts here

extends CharacterBody2D
var speed = 30
var damage = 10
var hits = 4
var kbpwr = 2000
var take_kb = false
var timed = false
var mov = false
var default = self.modulate
var dam = Color.RED
@onready var player = get_parent().get_parent().get_node("Phoenix")
func _physics_process(delta):
	if mov == true:
			velocity = (player.get_global_position() - position).normalized() * speed * delta
func _on_player_intersector_body_entered(body):
	if body.name == "Phoenix":
		player.playerData.hp -= damage
func _on_player_intersector_area_entered(area:Area2D):
	if area.name == "sword":
		hits -= player.playerData.power
		mov = false
		if hits <= 0:
func knockback(who):
	if who == "player":
			var kbdir = (velocity - Vector2(0,0)).normalized() *kbpwr
			player.velocity = kbdir
func _on_sight_body_entered(body):
	if body.name == "Phoenix":
		mov = true
func _on_sight_body_exited(body):
	if body.name == "Phoenix":
		mov = false
func damagee():
	$boko_sprite.modulate = dam
	await get_tree().create_timer(.3).timeout
	$boko_sprite.modulate = default
	await get_tree().create_timer(.3).timeout
	$boko_sprite.modulate = dam
	await get_tree().create_timer(.3).timeout
	$boko_sprite.modulate = default
func _on_timer_timeout():
	mov = true


extends CharacterBody2D
var last_mov = Vector2(0,0)
var cool = false
var move = true
var mov
var save_file_path = "user://save/"
var save_file_name = "Player.tres"
var playerData = SaveData.new()
@export var enable_cam = true
@onready var bar = get_node("CanvasLayer/hpbar")
func load_sav():
	playerData = ResourceLoader.load(save_file_path+save_file_name).duplicate(true)
func save():
func _ready():
	global.player = self
	if global.just_entered_building == true:
		global.just_entered_building = false
	if enable_cam == false:
		$cam.enabled = false
func  verify_save_directory(path):
func _physics_process(_delta):
	if global.entering_building == true:
		global.entering_building = false
	if global.tp_pos != Vector2(0,0):
		self.position = global.tp_pos
		global.tp_pos = Vector2(0,0)
	bar.value = playerData.hp
	var x = Input.get_action_strength("right") - Input.get_action_strength("left")
	var y = Input.get_action_strength("down") - Input.get_action_strength("up")
	if move == true:
		mov = Vector2(x,y)
	elif move == false:
		mov = Vector2(0,0)
	if mov == Vector2(0,0):
	if mov == Vector2(1,0):
	elif mov == Vector2(-1,0):
	elif mov == Vector2(0,-1):
	elif mov == Vector2(0,1):
	elif velocity == Vector2(0,0) and last_mov == Vector2(playerData.speed,0):
	elif velocity == Vector2(0,0) and last_mov == Vector2(-playerData.speed,0):
	elif velocity == Vector2(0,0) and last_mov == Vector2(0,-playerData.speed):
	elif velocity == Vector2(0,0) and last_mov == Vector2(0,playerData.speed):
	velocity = mov.normalized()*playerData.speed
	if Input.is_action_just_pressed("sword") and cool == false:
func last_mov_pos():
	if velocity != Vector2(0,0):
		last_mov = velocity
func sword():
	if playerData.has_sword == true:
	elif playerData.has_sword == false:
	velocity = Vector2(0,0)
	move = false
	if last_mov == Vector2(playerData.speed,0):
		$sword.rotation_degrees = 0
		await get_tree().create_timer(.5).timeout
	elif last_mov == Vector2(-playerData.speed,0):
		$sword.rotation_degrees = 180
		await get_tree().create_timer(.5).timeout
	elif last_mov == Vector2(0,playerData.speed):
		$sword.rotation_degrees = 90
		await get_tree().create_timer(.5).timeout
	elif last_mov == Vector2(0,-playerData.speed):
		$sword.rotation_degrees = 270
		await get_tree().create_timer(.5).timeout
	cool = true
	move = true
func _on_sword_cooldown_timeout():
	cool = false
func _process(_delta):
	if Input.is_action_just_pressed("dbg"):
	if Input.is_action_just_pressed("dbg2"):

bad code ik but ill be fixing it all later

1 Like

scene tree


1 Like

I suspect it’s this line

velocity = (player.get_global_position() - position).normalized() * speed * delta
#                                         ^ change to global position

But this would only fix it if the enemies are place as a child of an offset node, which could be happening on your main/world scene. The alternative fix would be to remove that offset if it exists.

i can try and find the tutorial i used for enemy ai if you need that

ill check

nvm i found it testing now

that works! self is implied, so self.global_position == global_position

tysm this works ill reopen this if it has more problems

i just like using self sometimes lol

me too, more personal :^)

1 Like

@gertkeno just for learning for next time whats the diff between global_position and position

Moving the parent of a Node will move all of it’s children, but the children’s position will remain the same, because it’s relative to the parent. The global_position is relative to 0,0 it contains all transforms from parents combined.

1 Like

thx i think i got it now