im new to godot and programming and for some reason my enemy isn't following my player, only going to the left what can i do to fix this?

extends CharacterBody2D

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

func _physics_process(delta):
	#enemy gravity
	velocity.y += gravity * delta
	if chase == true:
		player = get_node("../../player/player")
		var direction = (player.position - self.position).normalized()
		if direction.x > 0:
			get_node("Enemy").flip_h = true
		else:
			get_node("Enemy").flip_h = false
		velocity.x = direction.x * speed
	else:
		velocity.x = 0
	move_and_slide()

func _on_play_detection_body_entered(body):
	if body.name == "player":
		chase = true

func _on_play_detection_body_exited(body):
	chase = false
  • lumoncholy replied to this.
  • lumoncholy Enemy and player collision shapes and sprites must be at the origins of their respective character body nodes. Otherwise the numeric values of instanced scene coordinates will not actually match the sprite/collider appearance. This is why your calculated distances actually do not represent what is seen on the screen, hence producing strange behavior.

    Place ~~~ on a line before and after the code so that it will be displayed correctly.

    One obvious issue is that you are increasing velocity.y but never decreasing it.

    Also, it's inefficient to call get_node() in _physics_process(), which runs 60 times a second. It's better to do that in _ready() and save the reference in a variable.

    Do you get any error or warning messages?

      While your code is running print out the "direction" variable to make sure it's a value that seems right.

        Erich_L it seems right until i get too close on the left to the enemy, which is when the enemy starts going left instead of right

        Do nothing if the length of non-normalized direction vector is below certain threshold value.

          xyz uhh the non-normalized direction is -77, so what should i do?

          The length of the non-normalized direction vector is:
          (player.position - self.position).length()

          It can't be negative.

            DaveTheCoder ah sorry i was doing the wrong thing, but whenever i switch it to .length() it tells me the if direction.x > 0: is invalid and crashes

              lumoncholy whenever i switch it to .length()

              Switch what to length()? xyz's suggestion was to use the length(), which returns a float, as an additional test. You still need a Vector2, which normalize() returns, to access the .x or .y component.

                DaveTheCoder ahh sorry, misunderstanding. still figuring out how this all works. i ran the test, i think properly this time, and its either 1 or .999

                Player and enemy character nodes are not in the same coordinate space. Check that mobs and player(parent) have zeroed out transforms, or better yet, eliminate them completely as they are redundant and could cause more "bugs" like this in the future. Use groups instead. If you simply must have them, then it'd be best to do all distance/direction calculations in the global coordinate space.

                  xyz i got rid of the mobs and player(parent) nodes, but its still acting the same. am i misunderstanding something?

                  • xyz replied to this.

                    xyz
                    the enemy will go right, but it wont touch the player and will start going left once the player gets too close. but on the left side its behaving like it should be

                    • xyz replied to this.

                      lumoncholy Hard to tell from the video alone without inspecting the scene setup. Please attach the project if you can.

                      uhh, im not exactly sure how to do that. sorry abt this, didnt think following a tutorial would be this convoluted

                      • xyz replied to this.