Search code examples
c#unity-game-enginepath-findinga-star

A-Star Unity 2D: How to find vector2 coordinates of AI's current waypoint?


[Using A-Star project] Hi. So the problem is in the title basically. I have a top-down game in which the enemy should face the direction they're going. I've tried:

  1. To calculate Enemy's force of it's RigidBody in FixedUpdate
  2. To calculate the vector from enemy to target.

In the first instance Enemy changes its animation states too quickly, every fixed frame there's a new force applied (especially annoying when the AI is close to target). In the second Enemy always faces its target ignoring any obstacles. It's wallhacking, if you will. To solve this bastard I decided to find AI's current waypoint and I do not know how to do that. I've found steeringTarget method in A-Star's documentation, but I couldn't figure out how to implement it. I would REALLY appreciate any help. Thanks in advance! (steeringTarget method) https://arongranberg.com/astar/documentation/dev_4_1_0_9f8b6eb7/class_pathfinding_1_1_rich_a_i.php#a399e2bebfc8dfaf4fd291f051ca486e6


Solution

  • So thanks to the guy that tried to help, but that was not it. I solved my problem this way.

    Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
    

    So this is the vector that's needed. From this game object to the end of the waypoint. Your code may vary.

    Vector2 force = direction * speed * Time.deltaTime;
    

    And this is the variable that is needed. So we set the animator with force.x and force.y and this should work. I hope I made this at least somewhat clear. But if not - ask away.