Is there a way to properly get the direction?

Am using this package for a 2d game but am trying to grab the direction the player is walking in (N/E/S/W) or (NW/N/NE/E/SE/S/SW/W) if tried to look on google or on the forums but i have not really find a proper answer

Hi

You can use the ai.velocity property. (where ai is for example an AIPath instance, but it works for any movement script).

You got an example of how i would use it? or is it somewere in the example scenes? Because i did see you answer this before but i didnt understand it

As if i print the velocity in the console i get this

Those are direction vectors, means the direction is going basically left,
Vector3 (x,y,z) ->. your x parameter is negative so its going left,

You can normalize the vector using Vector3.Normalize, and then use Quaternion with:
Quaternion quat= Quaternion.LookRotation(v3Normalized);
then you will have the rotation the element has.

How you you want to see them represented?

1 Like

If you just want to snap an angle to the closest 45 or 90 degree angle you can use the next piece of code:

Vector2 agentDirection = new Vector2(ai.velocity.x, ai.velocity.z);
float snapAngle = 45;

float angleRad = Mathf.Atan2(agentDirection.x, agentDirection.y);
float angleDeg = angleRad * Mathf.Rad2Deg;

float resultAngle = Mathf.RoundToInt(angleDeg / snapAngle) * snapAngle;

resultAngle will be either 0, 45, 90, 135, 180 or negative those values.

If you want to make it snap to the closest 90 degree angle replace 45 with 90.
shouldn’t be too hard to convert those to NW N NE …

1 Like