forked from savagame/C-ProgrammingUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirection.cs
21 lines (19 loc) · 885 Bytes
/
direction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//2D Rotation in the direction of target.
//Author: Przemyslaw Zaworski
using UnityEngine;
using System.Collections;
public class direction : MonoBehaviour
{
public GameObject target;
void Update ()
{
Vector2 source = new Vector2(transform.position.x,transform.position.y);
Vector2 destination = new Vector2(target.transform.position.x,target.transform.position.y);
float delta = Mathf.Atan ((destination.x - source.x) / (destination.y - source.y));
float angle = delta * Mathf.Rad2Deg;
if (((destination.x - source.x) > 0.0f) && ((destination.y - source.y) < 0.0f)) angle = angle + 180.0f;
if (((destination.x - source.x) < 0.0f) && ((destination.y - source.y) < 0.0f)) angle = angle + 180.0f;
if (((destination.x - source.x) < 0.0f) && ((destination.y - source.y) > 0.0f)) angle = angle + 360.0f;
transform.rotation = Quaternion.Euler(0, 0, -angle);
}
}