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