on this code i wanted to calculate the direction of where is the player and go against it but it doesn't work because it goes only to the right even if i am aproching the detector on the left side, can anyone help me pls?

`extends CharacterBody2D

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var player
var chase = false
var SPEED = 50

func _physics_process(delta):
velocity.y += gravity * delta

if chase == true: 
	get_node("AnimatedSprite2D").play("Jump")
	player = $"../../Player/player"
	var direction = (player.position - self.position).normalized() 	
	print (direction)
	if direction.x > 0:
		get_node("AnimatedSprite2D").flip_h = true
	else:
		get_node("AnimatedSprite2D").flip_h = false
	velocity.x = direction.x * SPEED
else:
	get_node("AnimatedSprite2D").play("Idle")
	velocity.x = 0
move_and_slide()	

func _on_player_detection_body_entered(body):
if body.name == "Player":
chase = true

func _on_player_detection_body_exited(body):
if body.name == "Player":
chase = false
`
https://clipchamp.com/watch/hVNFm67PIIT

  • Toxe replied to this.

    leo050505 I don't know how your scene is organized but try using the global position of the player and the enemy.

    var direction = (player.global_position - self.global_position ).normalized()

    If that doesn't work then you might need to show some screenshots, more code or show/describe how your scene is structured.

      leo050505 @Toxe's solution will likely fix it as as the nodes have parent's that will affect their position versus the global_position. Did you try this?

      I can recreate it as posted and changing to global_position fixes it.

        leo050505 Why do you have two player nodes in your scene, "Player" and "Player/player"?

        You are getting the position of this one: player = $"../../Player/player"

        Is that the player node that is actually moving?

          leo050505 then there's something else wrong and worth verifying your code against the source material as that is unlikely to be incorrect (also, what tutorial?). Zip up and share your project here, I'm sure it just needs a fresh pair of eyes.

            Toxe yes the player with the lowrercase p is the one i want to move, so i think i am getting the right player

            Player is the mother of the other player as you can see on the picture

            spaceyjase the tutorial is

            but my zip file is 11 mb so i cant share it i think because on the godot forum i cant upload more than 2 mb, if you know an another method pls help me, thanks

            • Toxe replied to this.

              leo050505 but my zip file is 11 mb so i cant share it

              Make sure that you do not include the .godot folder. If that is still too big then you could make a simplified version by ripping all the assets and unnecessary scenes out of it because for finding the issue we don't need any fancy graphics or animations.

                leo050505 Alright, the problem is that you are not getting the position of the correct player node. Your code works if you change line 14 to:

                player = $"../../Player/player/Player"

                The problem is your weird player node setup. You have "Player/player" in World which is just a Node2D.

                And your player scene looks like this:

                Which is the same Node2D from the World scene at the top and below it is the actual CharacterBody2D "Player". Unless you have very good reasons to do it like this you should restructure your scenes so that the root node of your Player scene is a CharacterBody2D and you should also remove the first "Player" Node2D from World.

                There should only be one node called Player and it should be a CharacterBody2D. Basically you want your World scene to look like this:

                  Toxe Thanks omg!! It worked, and i learned new things, thank you very much again, you are very kind! 🙂