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
boko.gd

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
			move_and_collide(velocity)
func _on_player_intersector_body_entered(body):
	if body.name == "Phoenix":
		player.playerData.hp -= damage
		knockback("player")
func _on_player_intersector_area_entered(area:Area2D):
	if area.name == "sword":
		hits -= player.playerData.power
		damagee()
		mov = false
		if hits <= 0:
			self.queue_free()
func knockback(who):
	if who == "player":
			var kbdir = (velocity - Vector2(0,0)).normalized() *kbpwr
			player.velocity = kbdir
			player.move_and_slide()
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():
	$Timer.start()
	$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

Phoenix.gd

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():
	print("load")
	playerData = ResourceLoader.load(save_file_path+save_file_name).duplicate(true)
func save():
	print("save")
	ResourceSaver.save(playerData,save_file_path+save_file_name)
func _ready():
	global.player = self
	verify_save_directory(save_file_path)
	if global.just_entered_building == true:
		load_sav()
		global.just_entered_building = false
	if enable_cam == false:
		$cam.enabled = false
func  verify_save_directory(path):
	DirAccess.make_dir_absolute(path)
func _physics_process(_delta):
	if global.entering_building == true:
		save()
		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):
		pass
	if mov == Vector2(1,0):
		$AnimationPlayer.play("walk_right")
	elif mov == Vector2(-1,0):
		$AnimationPlayer.play("walk_left")
	elif mov == Vector2(0,-1):
		$AnimationPlayer.play("walk_up")
	elif mov == Vector2(0,1):
		$AnimationPlayer.play("walk_down")
	elif velocity == Vector2(0,0) and last_mov == Vector2(playerData.speed,0):
		$AnimationPlayer.play("idle_right")
	elif velocity == Vector2(0,0) and last_mov == Vector2(-playerData.speed,0):
		$AnimationPlayer.play("idle_left")
	elif velocity == Vector2(0,0) and last_mov == Vector2(0,-playerData.speed):
		$AnimationPlayer.play("idle_up")
	elif velocity == Vector2(0,0) and last_mov == Vector2(0,playerData.speed):
		$AnimationPlayer.play("idle_down")
	velocity = mov.normalized()*playerData.speed
	if Input.is_action_just_pressed("sword") and cool == false:
		sword()
	last_mov_pos()
	move_and_slide()
func last_mov_pos():
	if velocity != Vector2(0,0):
		last_mov = velocity
func sword():
	if playerData.has_sword == true:
		pass
	elif playerData.has_sword == false:
		return
	velocity = Vector2(0,0)
	$sword/coll.set_deferred("disabled",false)
	$player_sprite.hide()
	$sword_sprite.show()
	move = false
	if last_mov == Vector2(playerData.speed,0):
		$AnimationPlayer.play("sword_right")
		$sword.rotation_degrees = 0
		await get_tree().create_timer(.5).timeout
	elif last_mov == Vector2(-playerData.speed,0):
		$AnimationPlayer.play("sword_left")
		$sword.rotation_degrees = 180
		await get_tree().create_timer(.5).timeout
	elif last_mov == Vector2(0,playerData.speed):
		$AnimationPlayer.play("sword_down")
		$sword.rotation_degrees = 90
		await get_tree().create_timer(.5).timeout
	elif last_mov == Vector2(0,-playerData.speed):
		$AnimationPlayer.play("sword_up")
		$sword.rotation_degrees = 270
		await get_tree().create_timer(.5).timeout
	$player_sprite.show()
	$sword_sprite.hide()
	$sword/coll.set_deferred("disabled",true)
	cool = true
	$sword_cooldown.start()
	move = true
func _on_sword_cooldown_timeout():
	cool = false
func _process(_delta):
	if Input.is_action_just_pressed("dbg"):
		save()
	if Input.is_action_just_pressed("dbg2"):
		load_sav()

bad code ik but ill be fixing it all later

1 Like



scene tree

@gertkeno

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