Enemy not moving towards player with cartesian coodinates

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TR29

I have a 2 Kinematic Bodies 2D - Player and Enemy
Player moves in cartesian style as below:

func move_state(delta):	
var direction = Vector2()

if Input.is_action_pressed("ui_up"):
	direction += Vector2(0, -1)
elif Input.is_action_pressed("ui_down"):
	direction += Vector2(0, 1)
	
if Input.is_action_pressed("ui_left"):
	direction += Vector2(-1, 0)
elif Input.is_action_pressed("ui_right"):
	direction += Vector2(1, 0)
motion = direction.normalized() 
motion = cartesian_to_isometric(motion)

converting with this

func cartesian_to_isometric(cartesian):
screen_pos.x = cartesian.x - cartesian.y
screen_pos.y = cartesian.x / 2 + cartesian.y / 2
return screen_pos

this works fine.

The Enemy should move towards and it works as expected with a static 2D body on the respective layer. However when it detects the player, it moves up. If I change the code I can make it move in different directions, but I can’t make it follow the player from a 360 degree perspective. Weird is, that when the collision shapes of Enemy and Player interact, then the Enemy actually swings a bit around and after some seconds goes away.

This is the code from Enemy:

animationState.travel("Run")
animationTree.set("parameters/Run/blend_position", screen_pos)
var player = playerDetectionZone.player
if player != null:
accelerate_towards_point(player.global_position, delta)

    else:
        state = IDLE

    velocity = move_and_slide(velocity)

    func accelerate_towards_point(point, delta):
    var dir = global_position.direction_to(point)
    velocity = velocity.move_toward(dir * MAX_SPEED, ACCELERATION * delta)
    dir = cartesian_to_isometric(velocity)

The animation tree changes as it should, I also tried with extra variables and some different code but seems I can’t make it work.

   dir = (player.global_position - global_position)

It seems there must be a different calculation for the vectors, so the Enemy can align with Player, but not sure which approach would be good here.
Any ideas are highly appreciated.

EDIT

I wrecked my brain a couple of days to understand what was the issue. It had actually nothing to do with the vectors.
I might have run into a possible bug. I just had to move a state in the animation tree then run the game and the Enemy was able to follow me as it should. I cannot reproduce this behavior, but probably some chain of actions within the animation tree was the cause.
However, it somehow affected the vectors as well. When I replaced the movement in isometric mode with the one you would get straight from arrow keys, the enemy was moving away under another angle. FYI, maybe this helps anyone debug this in future :slight_smile: