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

Time:07-16

[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

CodePudding user response:

For questions related to A* Pathfinding Project it may be better to ask on the developer forum.

But here's my experience using it.

Usually you start with a seeker.

public Seeker seeker;

Since pathfinding calculations are asynchronous you must register for an event that tells us when the path is ready.

seeker.pathCallback  = OnPathComplete;

Somewhere in your code you call to search for a path.

seeker.StartPath(start, end);

When the path is ready you will get a Path object. It contains all of the points and vectors.

List<GraphNode> path;

List<Vector3> vectorPath;    

You can use the built-in PathInterpolator to smooth out the movement.

When you get the new path pass the vector path to the interpolator.

interpolator.SetPath(path.vectorPath);

On FixedUpdate you need to interpolate and apply the movement.

interpolator.MoveToCircleIntersection2D(transform.position, 0.1f, GraphTransform.identityTransform);
var moveDir = (interpolator.position - this.transform.position).normalized;
// Apply moveDir to Rigidbody, or whatever.

CodePudding user response:

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.

  • Related