So basically I’m doing a 2D-game which is from top-view and because I am pretty new to Unity and scripting in general, I don’t have a clue how do I get the direction of a moving object (in this case, an enemy) so I can animate it.
I just want to know how do I know whether the object is going up, down, left or right.
This is my script at the moment. I really appreciate every helping comment, but most of all I would like to know the easiest way to know the direction.
int movementSpeed = 3;
float detectionRange = 3f;
Transform playerTransform;
Transform myTransform;
void Awake ()
{
myTransform = transform;
}
// Use this for initialization
void Start () {
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update () {
if (Vector3.Distance(playerTransform.position, myTransform.position)<detectionRange)
{
//goes to the players' direction
Vector3 dir = playerTransform.position-myTransform.position;
dir.Normalize();
myTransform.position += dir * movementSpeed * Time.deltaTime;
}
}