0 votes

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:

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

        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 :)

in Engine by (12 points)
edited by

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.