Decided to go with a completely different system of movement and pathfinding
How do I get my Ai agent to rotate properly according to the direction of its movement?
I am using NavMeshPlus (https://github.com/h8man/NavMeshPlus) to make an ai 'patrol' from one 'checkpoint' to another (target01 and target02) and back on a 2D, top-down map. The actual movement from one to the other works fine, however I need it to rotate towards the direction it is moving. The problem I am having is that, after hours of trying different approaches, I can't seem to get the sprite to rotate about the z-axis, but for some reason can get it to rotate 90 degrees on the y axis (so that it is perpendicular to the background), and rotate on the x axis based on it's direction of movement. I'm aware this is probably a trivial issue but as someone with little experience in both unity and C# it has me completely stumped.
Attached is my current code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class RoweBotMovement : MonoBehaviour
{
[SerializeField] Transform target01;
[SerializeField] Transform target02;
//ignore //[SerializeField] Transform playerLastSeen;
Transform currentTarget;
[SerializeField] float currentWaitTime;
//true = target01, false = target02
bool whichTarget;
NavMeshAgent agent;
void Start()
{
//Setup stuff of AI (stops it going mental)
agent = GetComponent<NavMeshAgent>();
agent.updateRotation = false;
agent.updateUpAxis = false;
//
currentTarget = target01;
StartCoroutine(SetAiTarget());
}
void Update()
{
//if agent reaches its target, change to the other target
float remaining = agent.remainingDistance;
if (remaining <= 0f)
{
whichTarget = !whichTarget;
switch (whichTarget)
{
case true:
currentTarget = target02;
break;
default:
currentTarget = target01;
break;
}
StartCoroutine(SetAiTarget());
}
}
//set target of RoweBotTM + wait for certain time at each checkpoint
public IEnumerator SetAiTarget()
{
yield return new WaitForSeconds(currentWaitTime);
agent.SetDestination(currentTarget.position);
}
}