Code to detect if NavMeshAgent is moving left or right in isometric game. (Image attached)

The project is an isometric 2.5d game.

I have NavMeshAgent with a 2D Sprite bone rig as a child to it.

Basically if the agent is moving right I want him to face right and vice versa for when he is moving left.

I have included an image so you can see where the camera is relative to the Z and X axis:

6980939--823625--unity-agent.png

Also worth noting I have set angular speed to 0 as I don’t want the the agent rotating the sprite.

ATM I have this code but it doesn’t really work:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AnimateAgent : MonoBehaviour {

    //Set in inspector
    public NavMeshAgent agent;
    public GameObject Model;

    Vector3 localScale;
    bool movingRight;
    //Character starts facing left by default, just because of the way the prefab is set up
    bool prevMovingRight = false;

    void Start() {
        localScale = Model.transform.localScale;
    }

    // Update is called once per frame
    void Update()    {

        if(agent.desiredVelocity.x > 0 && agent.desiredVelocity.z < 0) {

            movingRight = true;

        } else {

            movingRight = false;

        }

        if(prevMovingRight != movingRight) {

            //Flip Character
            localScale.x *= -1;
            Model.transform.localScale = localScale;

        }

        prevMovingRight = movingRight;

    }

}

So atm it sort of works sometimes, I think it’s to do with the desiredVelocity if statement being wrong.

Appreciate any help with this. Also would love any tips to improve my coding.

TIA

It’s best not to have a boolean that actually SAYS left and right, then use it to FLIP something.

What if the two get out of sync somehow, or didn’t start “in sync” to begin with?

How about:

Vector3 scale = Vector3.one;

if (movingRight)
{
  scale.x = -1;
}

transform.localScale = scale;

See how that cannot get out of sync? Now I might have it backwards there (in fact I bet it is backwards), but if it is, then change line 3 above to be if (!movingRight) instead.

1 Like

Hi Kurt, thanks for your reply really appreciate it.

I’m actually using the prevMovingRight bool to store the direction in the previous update.

So…

        if(agent.desiredVelocity.x > 0 && agent.desiredVelocity.z < 0) {
            movingRight = true;
        } else {
            movingRight = false;
        }

To work out the direction, then…

if(prevMovingRight != movingRight) {
            //Flip Character
            localScale.x *= -1;
            Model.transform.localScale = localScale;
        }

        prevMovingRight = movingRight;

So if the the direction has changed, then flip the character.

I thought the problem was probably to do with this line here?

if(agent.desiredVelocity.x > 0 && agent.desiredVelocity.z < 0)

So it’s wrong when desiredVelocity x and z are both above 0, or both below 0, but can’t get my head around it.

Sorry if I am not understanding your post properly. I am grateful for the help!

Move around and print out (using Debug.Log()) what the value of movingRight is.

1 Like

OK got it working consistently now. Changed the 0’s to 2’s.

if(agent.desiredVelocity.x > -2 && agent.desiredVelocity.z < 2) {

Not totally sure why it works.

I noticed in Debug.Log that the max any value can be for desiredVelocity is 4 (minimum being -4).

Then tried out a few different numbers between 4 and -4 and this seems to work.

Thanks for your help!