Horje
know to which direction Vector.MoveTowards is moving unity 2D Code Example
know to which direction Vector.MoveTowards is moving unity 2D
Vector2 v = target_position - current_position; //Get our target and current positions (this.gameObject.transform.position).
float a = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg; //Use Mathf.Atan2 method to get the radians.
//This will give you the radians of the +x axis in 2d. So 0 means it's facing right, pi is left, pi/2 is down, and 3pi/4 is up.
//Then multiply it with Mathf.Rad2Deg, which will give us degrees, and then it'd be 0, 180, 90, and 270 respectively.
int index = (int)((Mathf.Round(a / 90f) + 4) % 4); //Round off to the nearest 90 degree to eliminate diagonal movement.
//If you want diagonal, skip it. ^^

//After all this, index will be - 0 for right - 1 for up - 2 for left - 3 for down 
//Finally set your animations with if(index == 1){ //etc...




Csharp

Related
c# how to check if two lists have same values Code Example c# how to check if two lists have same values Code Example
string format comma c# Code Example string format comma c# Code Example
the really long string variable is a string in which a very long string is stored Code Example the really long string variable is a string in which a very long string is stored Code Example
how to display doubles with trailing zeros in c# Code Example how to display doubles with trailing zeros in c# Code Example
gcd of number Code Example gcd of number Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
12