I have a 2 Kinematic Bodies 2D - Player and Enemy
Player moves in cartesian style as below:
var direction = Vector2()
direction += Vector2(0, -1)
direction += Vector2(0, 1)
direction += Vector2(-1, 0)
direction += Vector2(1, 0)
motion = direction.normalized()
motion = cartesian_to_isometric(motion)
converting with this
screen_pos.x = cartesian.x - cartesian.y
screen_pos.y = cartesian.x / 2 + cartesian.y / 2
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:
var player = playerDetectionZone.player
if player != null:
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.
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 :)