Mob cannot print out correct direction of player

4.2 Godot Version

I’m pretty new to the community and Godot in general so I was following a Godot crash course when I encountered this error.

Here is the crash course btw. The error (59:45) I encountered was that every time I approached my mob, no matter what, it would print “Right”. Here is my code in my mob:

func _on_player_detection_body_entered(body):
	if body.name == "Player":
		player = get_node("../../Player/Player") #<-path to player
		var direction = (player.position - self.position).normalized()
		if direction.x > 0:
			print ("Right")
		else:
			print ("Left")

Any thoughts?

You should be comparing against global_position not position, assuming the player’s collision body and sprite is at the same position as the player node itself, and the frog’s detection zone is also centered on the frog that should be working

var direction = player.global_position - self.global_position

If that works where using position didn’t it means one of the nodes above either the frog or the player in the tree was adding some global position, so their local positions are no longer relative to each other

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.