Support Forum

Am I misunderstanding agent.velocity?

I am using the following code to calculate where an arrow should travel based on target position + target velocity: http://wiki.unity3d.com/index.php/Calculating_Lead_For_Projectiles

For some reason, using IAstarAI.velocity gives incorrect results when agent is moving around in some random cases. However if I use this method I made my self it works like 99% of the time, is not IAstarAI.velocity correct in-parameter when I need the current speed of the target agent?

Custom method that works decently, but obviously not perfect since it uses maxSpeed.

 public Vector3 GetSpeed()
    {
        Vector3 aim = agent.destination - transform.position;

        if (agent.velocity.magnitude < .1f)
            return Vector3.zero;

        return aim.normalized * agent.maxSpeed;
    }

Hi

agent.velocity should work fine. However, note that it includes gravity. You may want to zero out the y coordinate.

You might also be interested in https://arongranberg.com/astar/docs/iastarai.html#steeringTarget