Edit: I uploaded a Youtube video demonstrating the issue I’m describing. Hopefully this will help to convey the problem. - YouTube
My title is a little misleading - I actually have one bit of code that behaves drastically different in two different scripts. One script simply moves my character to a RaycastHit.point - the rotation is working perfectly here. Basically the character runs to a corner of the NavMeshPath before turning, and it looks very natural.
public void MoveWithMouse()
{
myTransform = transform;
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
position = hit.point;
target = hit.transform;
Quaternion newRotation = Quaternion.LookRotation(position - myTransform.position);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
navMeshAgent.stoppingDistance = 0.5f;
if (animator.GetBool("isWalking") == false)
{
animator.SetBool("isWalking", true);
animator.Play("HumanoidRun");
}
navMeshAgent.SetDestination(hit.point);
}
}
In the other script, the character is moving to a position set by an earlier RaycastHit. I have to use an IEnumerator function because I need the function to wait until the next frame before it actually does anything, otherwise the NavMeshAgent is wonky as hell (if it moves at all). The same code used here causes the character to turn toward the final corner of the path very slowly. I suspect this has to do with me needing to call navMeshAgent.Resume(), but I’m stumped trying to figure out how to solve this.
public IEnumerator BattleMove()
{
//logic for moving in battle. Should consume action points based on units traversed.
//need a way to calculate path, and report number of units a move would take (thus calculating AP)
//the calculation needs to take place before the player actually moves, so they know how much AP it will take
yield return new WaitForEndOfFrame();
navMeshAgent.stoppingDistance = 0.5f;
myTransform = transform;
Quaternion newRotation = Quaternion.LookRotation(position - myTransform.position);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
navMeshAgent.SetDestination(position);
moving = true;
navMeshAgent.Resume();
if (animator.GetBool("isWalking") == false)
{
animator.SetBool("isWalking", true);
animator.Play("HumanoidRun");
}
if (Vector3.Distance(position, transform.position) <= navMeshAgent.stoppingDistance + 2 && moving)
{
animator.SetBool("isWalking", false);
moving = false;
}
if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
{
currentAction = playerAction.PENDING;
}
}
I’m extremely stuck on this problem and any suggestions are greatly appreciated. Thank you.