NPC facing direction

Hello,

I am stuck, and i know many probably have already accomplished this, but maybe mine has a small slight twist… or not… so here goes, because I am stuck.

I made a top down 2d NPC or NPC’s that go in a set patter, not random like i have seen on youtube or some online tutorials, and i made empty objects that act like beacons or locations for the npc to go, and i set 4, so it goes in a nice complete square… or rectangle… whatever, but i do that so some go longer or short distance depending on the location like so in C# code:

//Variables move and Transform is array of 4 spots for npc to move to:

    public float MinCountdown = 10;
    public float MaxCountdown = 14;
    public float TimeCountdown = 5;
    private int currentMoveDirection;
    public Transform[] targetSpots;

//Method for NPC angle to move...

    private void Animation_Angle(float x, float y)
    {
        anim.SetFloat("Vertical", x);
        anim.SetFloat("Horizontal", y);
    }


    void Update()
    {
         ///there is a count down timer but for the sake of time and space, the IF the TimeCountdown reaches 0 then do this IF statement below:

        if (currentMoveDirection <= (targetSpots.Length - 1))
        {
            transform.position = Vector2.MoveTowards(transform.position, targetSpots[currentMoveDirection].position, moveSpeed * Time.deltaTime);

            Vector2 dir = targetSpots[currentMoveDirection].position - transform.position;

Animation_Angle(dir.y, dir.x);
}
    }

The problem I am having is, When the NPC reaches its destination, it faces down, or 0, therefore goes back to the direction of down or the default… and I dont want that honestly, i would like it to stay looking at the direction it went to… example:

if NPC was walking left then after it reaches the left destination… it looks straight back down, because the dir.x and dir.y is 0… but wish i can set it or have it were it would just stay looking left, until the next part of the location array to go to that location… then face that way… etc… hope this all makes sense… not sure what i am doing wrong, maybe a simple setting… or not?

I tried just now to use:

    public void Animation_Movement(float dirx, float diry)
    {
        float angle = Mathf.Atan2(diry, dirx) * Mathf.Rad2Deg; // Get Angle of Joystick for Idle

        Debug.Log(angle);

}

however since there are several NPC’s, all show different numbers… so nothing that is solid straight forward angle numbes… :frowning:

Maybe only call Animation_Movement if the direction is big enough?

if (dir.magnitude > 0.1f)
{
   Animation_Angle(dir.y, dir.x);
}

I love making little patrol scripts… it’s like creating life when your little dude(s) start trucking around and doing stuff while you play the game.

Kurt-Dekker, YES, at first, i sorta hated it, like trying to just make move… but as a player, when i play my game, and I see the movement… its like “I MADE LIFE!!!”

lol

but thank you again Kurt-Dekker, you are a genius and it worked :slight_smile:

1 Like