0 votes

I have a follow script on my enemy which works but i cant seem to get the sprite directions correct when following the player

the current script for the sprite direction i use is

if player_direction_x>0 and player_direction_x>player_direction_y: spritedirection=right

if player_direction_y>0 and player_direction_y>player_direction_x spritedirection=down

if player_direction_x<0 and player_direction_x<player_direction_y: spritedirection=left

if player_direction_y<0 and player_direction_y<player_direction_x spritedirection=up

as you can see its not very good

If any of you guys have any knowledge on how to do this please send an example
Thank you.

in Engine by (395 points)

1 Answer

0 votes

Your code doesn't make much sense out of context. What type of nodes are you using for the player and enemy? 2D nodes have a built-in direction property, so you don't need to make your own variables for this.

See the following tutorial in the docs for examples of 2D movement. There's a "follow mouse" example which you could use for this:

https://docs.godotengine.org/en/latest/tutorials/2d/2d_movement.html

by (20,897 points)

i already have a working follow player ai the problem is the sprite animation for it.

like is the player is above self, play walkup if the player is to the right of self, play walkright

I see. Again, without seeing your movement code, I can't really tell you. For example:

  • If the object is moving, how are you telling it what direction to move? You can probably set the animation that matches at the same time.
  • Are you allowing diagonal movement or only up/down/left/right? This would make things slightly more complicated.

Post your code so we can see what you have. Then it will be possible to answer your question.

Hello again busy with school stuff any way here's the code that is for the movement and sprite animation

it extends kinematicbody2d

func follow_loop()://FOLLOW AI
        if turntime>0:
            turntime-=1
        var target = get_parent().get_node("Player")
        var direction = target.get_global_position() - self.get_global_position()
        var to_player = target.global_position - self.global_position
        distance = to_player.length()
        if distance < blindspot and distance > atkrange:
            var X=int(direction.x)
            var Y=int(direction.y)
            if turntime==0:
                if Y>0 and Y>X:
                    turntime=10
                    movedir=dir.Down
                if X<0 and X<Y:
                    turntime=10
                    movedir=dir.Left
                if Y<0 and Y<X:
                    turntime=10
                    movedir=dir.Up
                if X>0 and X>Y:
                    turntime=10
                    movedir=dir.Right
            if X==Y and X>0 and Y>0:
                movedir=Vector2(1,1)
            if X==Y and X<0 and Y<0:
                movedir=Vector2(-1,-1)
            if X>0 and Y<0:
                movedir=Vector2(1,-1)
            if X<0 and Y>0:
                movedir=Vector2(-1,1)
            motion=direction.normalized()*SPEED
            movement_loop()
        else:
            movedir=Vector2(0,0)

    func movement_loop():// MOVEMENT
        if CLASS=="BOSS":
            motion=movedir.normalized()*SPEED
        else:
            if hitstun==0:
                motion=movedir.normalized()*SPEED
            else:
                motion=knockdir.normalized()*knockspeed
        move_and_slide(motion, Vector2(0,0))

    func spritedir_loop()://CHECKS DIRECTION WHERE KINEMATIC BODY IS MOVING
        match movedir:
            Vector2(0,-1):
                spritedir="Up"
            Vector2(0,1):
                spritedir="Down"
            Vector2(-1,0):
                spritedir="Left"
            Vector2(1,0):
                spritedir="Right"

    func anim_switch(animation):// CHANGES ANIMATION
        var newanim=str(animation,spritedir)
        if $anim.current_animation!=newanim:
            $anim.play(newanim)

It is my script i use for all my kinematic bodies

i call them with a matchstatement in a physics_process function

Ex.

func _physics_process(delta):
    match state:
        "default":
            state_default()
        "attack":
            attack()

func state_default():
    print(health)
    damage_loop()
    follow_loop()
    spritedir_loop()
    if movedir!=Vector2(0,0):
        anim_switch("Walk")
    else:
        anim_switch("Idle")
    if distance<atkrange:
        state="attack1"

func attack():
    damage_loop()
    anim_switch("Attack")
    movedir=dir.Center

I hope this isnt too much for you
Its nothing really important just a hobby i do.

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.