2D Enemy Movement - Getting X and Y Axis Values from A*

I have what may be a basic question but can’t quite find what I am looking for. I have my enemy set up to seek the player and this works fine.

However, my enemy animations are based on their movement on the x and y axis.

My current logic that I am using is as follows:

private Character character;
private Vector2 axis;

void Start(){
     this.character = this.GetComponent<Character>();
     this.axis = new Vector2();
}

void FixedUpdate(){
     this.axis.x = WhatShouldBeHereFromAStar?;
     this.axis.y = WhatShouldBeHereFromAStar?;

     bool isHorizontalStillPressed = true;

     // Move the character using the axis as the input
     this.character.Move(this.axis, isHorizontalStillPressed);
}

Any ideas how I can get that X and Y axis value from AStar?

Thank you kindly for any insights that you can provide. :slight_smile:

Hi

If you want to get the agent’s current velocity and use that to determine the animation, then what you do is:

var ai = GetComponent<IAstarAI>();
var dx = ai.velocity.x;
var dy = ai.velocity.y;

See IAstarAI - A* Pathfinding Project

Thank you Aaron and I hope you feel better soon.

I am currently getting the x and y values from the component and it does sort of work. Is this the same as above? I feel like your usage of is the better (proper) way to go yes?

Hi

I’m not quite sure what you mean?

Hey sorry, it seems like some of my comment was stripped out.

So I found some code and am doing it this way:

myPath = this.GetComponentInParent<AIPath>();

and then work with myPath.velocity.x etc.

What difference is there with AIPath vs IAstarAI as shown in your example? Is one better than the other or more correct?

IAstarAI is an interface implemented by all movement scripts (including AIPath). So AIPath and IAstarAI are interchangeable here.

Excellent. Is one more performant?

Nah. It boils down to the exact same method calls. They are functionally identical, and the performance should be the same.