Skip to content

Instantly share code, notes, and snippets.

@Sammyjroberts
Created October 17, 2016 20:57
Show Gist options
  • Save Sammyjroberts/cf02607b2fca4d081e007e19afb30aa2 to your computer and use it in GitHub Desktop.
Save Sammyjroberts/cf02607b2fca4d081e007e19afb30aa2 to your computer and use it in GitHub Desktop.
public class LockOn : MonoBehaviour {
GameObject target = null;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(target != null) {
var dir = target.transform.position - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
void OnTriggerEnter2D(Collider2D col) {
Debug.Log("asdsd");
if(col.tag == "Player") {
Debug.Log("ey");
target = col.gameObject;
}
}
void OnTriggerExit2D(Collider2D col) {
target = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment