Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

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...
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to make an object appear and disappear in unity 
Csharp :: c# write byte[] to stream 
Csharp :: deserialize object to dictionary c# 
Csharp :: triangle minimum path sum c# 
Csharp :: c# cast to type variable 
Csharp :: read folder c# 
Csharp :: enum get all values c# 
Csharp :: c# dictionary loop key value 
Csharp :: visual studio fix formatting 
Csharp :: movement unity 
Csharp :: linux command line switch statement 
Csharp :: c# append text to file 
Csharp :: C# get key by value Dict 
Csharp :: c# array to list 
Csharp :: c# md5 
Csharp :: dialog box with form flutter 
Csharp :: unity log warning 
Csharp :: linq query select top 1 c# 
Csharp :: converting bitmap to byte array c# 
Csharp :: C# tolower all in a array 
Csharp :: total months between two dates c# 
Csharp :: c-sharp - get current page url/path/host 
Csharp :: c# lambda join two tables 
Csharp :: add all elements in a list c# 
Csharp :: getcomponent unity 
Csharp :: string list to object array in c# 
Csharp :: find character from string c# count 
Csharp :: c# double to int 
Csharp :: roman to int 
Csharp :: public gameobject unity 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =