AI in 4 Direction

Hi everybody ! ( sorry for my bad english)

I’m trying to make a 2D rpg like a old Zelda game, But i have some trouble with the direction and AI of my monster.

I know how to make move my monster only on the 4 direction. But, i think about something, If my player is on a diagonal to my monster, and my monster beggin to chase him, it will make some trouble with animation and direction no ? My monster will go very fast (for exemple) on the right, then up, and right, and up …so the animation will be ugly and the direction will be not just in 4 direction no?

So i don’t know how to fixe that :confused: If someone can help me^^?

Have a good night !

Most 2D strategy games I’ve played just use the sideways animation while letting the character move diagonally. Is there a reason you can’t do that here?

(If you don’t have the resources to create the diagonal animations, that is)

I think you’ll definitely see people crying foul if they perceive the monster to be taking 2 turns instead of 1, though.

Thx for you’re anwser, actually in my game, ennemy can move in all direction, but i can’t sucess to set up (4 animation direction) I would like to set up the animation for the main direction but i have some trouble with up and down :confused:

So if i block the direction, i have no problem with my animation^^

But if you have a solution to set up animation with all direction i’ll really appreciate too ^^
i’m using movedirection.normalized.x and y and :

if (Control.moving == true)
        {

            if (Control.movedirection.normalized.x > 0)
            {
                if (Control.movedirection.normalized.x > Control.movedirection.normalized.y)
                {
                    anim.Play("Skull_RIght");
                }
                else if (Control.movedirection.normalized.y > Control.movedirection.normalized.x)
                {
                    anim.Play("Skull_Up");
                }
                else if (Control.movedirection.normalized.y < -0.5)
                {
                    anim.Play("Skull_down");
                }


            }
                else if (Control.movedirection.normalized.x < 0)
                {
                    if (Control.movedirection.normalized.x < Control.movedirection.normalized.y)
                    {
                        anim.Play("Skull_Left");
                    }
                    else if (Control.movedirection.normalized.y < Control.movedirection.normalized.x)
                    {
                        anim.Play("Skull_down");
                    }
                    else if (Control.movedirection.normalized.y > 0.5)
                    {
                        anim.Play("Skull_Up");
                    }


                }
        }

Looking at that code, it seems like the enemy can only move if the x move direction is non-zero. Is that what you really want? It doesn’t look like it covers the case of the enemy moving straight up or down (x is zero, y is non-zero). Maybe the first check should use >= instead of >, for example.

Secondly, in the (x > 0) block, you will always either play the Right or Up animation. Skull_down will never play.

For this logic to work better, it probably would look something like this:

if (y > 0.5) 
    anim.Play("Skull_Up");
else if (y < -0.5)
    anim.Play("Skull_Down"); 
else if (x > 0)
    anim.Play("Skull_Right"); 
else if (x < 0)
    anim.Play("Skull_Left"); 
else 
    anim.Play("Idle);

(I just threw that Idle check in there, you decide if you need it or not :slight_smile: )

However, if the target is perfectly diagonal to the enemy, then you might run into jittering if you use 0.5. I suppose this is the problem you were trying to describe in your first post? If so, then I think I understand what you’re asking now.

If this is a tile based game, then one easy answer is to use 0.45 instead of 0.5 (or 0.55 or whatever). However, if it’s a tile-based game, why use this logic? Just go with positive, negative, or zero checks.

If it’s not a tile-based game and you really do need 360 degree movement, then I’d add a timer so that the animation will only change if enough time has passed (like half of a second or something so you don’t keep flipping every frame).

As you mentioned you were making an old school Zelda game, I originally took that to mean you were creating a tile based game, where you can only move in one of 8 directions.

Finally, you’ll want to cache Control.movedirection.normalized instead of re-calculating it every time you need it.

Oh great ! you solve my problem with you’re directionnal code! :open_mouth: !!!

The animation is exactly what i want ! It’s perfect !

I 'll just do like you said with the 0.45 instead of 0.5 ! :slight_smile: It will be perfect ! :slight_smile:

Thx for you’re help ! and have a good day !

1 Like