Support Forum

Replacement for NavMeshAgent.nextPosition

I see there are a lot of questions on animation (How do i implement animation? and A* Pathfinding project with mecanim animation) and they seem to get the same answer being, “I should make an example for mecanim” and a link to check out this page: Unity - Manual: Coupling Animation and Navigation
I shortened down the code with help from a YouTube video and made it work for unity’s navmesh with just:

using UnityEngine;
using UnityEngine.AI;

public class MoveAnimatedCharacter : MonoBehaviour
{
    Animator animator;
    NavMeshAgent agent;
    Vector3 worldDeltaPos;
    Vector2 groundDeltaPos;
    Vector2 velocity = Vector2.zero;

    void Start()
    {
        animator = GetComponent<Animator>();
        agent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        worldDeltaPos = agent.nextPosition - transform.position;
        groundDeltaPos.x = Vector3.Dot(transform.right, worldDeltaPos);
        groundDeltaPos.y = Vector3.Dot(transform.forward, worldDeltaPos);
        velocity = Time.deltaTime != 0.025f ? groundDeltaPos / Time.deltaTime : Vector2.zero;

        animator.SetFloat("velX", velocity.x);
        animator.SetFloat("velY", velocity.y);
    }
}

But now I am trying to convert it to A* Pathfinding. Unfortunately, there is no agent.nextPosition so I can’t simply change the NavMeshAgent to IAstarAI.

So the problem line is:

worldDeltaPos = agent.nextPosition - transform.position;

I looked around for a bit and found an undocumented method in the IAstarAI being:

void GetRemainingPath(List<Vector3> buffer, out bool stale)

It says in the summary that it can be used to get the points and I was able to grab the next point with buffer[1]

I tried using it to replace the worldDeltaPos line and it somewhat worked. The AI turns alright and has the correct animations, but when they are meant to walk straight, they start freaking out, turning left and right very quickly. So, I don’t think I was right going with that.

So, is there any replacement here for agent.nextPosition that will work with this? Is there an easier way of doing this, or have you made the example you mentioned in those other questions, and I’m just too dumb to find it?

Hi

You might be interested in the script called MecanimBridge. It does something very similar to what you want to do, I think.

1 Like

Just tried using it and it appears to be working well. I just can’t seem to make them stop on the exact point they end on.

Edit: Literally just had to change the one public variable… I’m dumb sometimes.

1 Like