【Unity】対象へ振り向くLookAt2D

DEVELOP, Unity

普通に書いたら

public void LookAt2D(Transform self, Transform _target)
{
    var direction = (_target.position - self.position);
    direction.Normalize();
    var rotZ = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    self.rotation = Quaternion.Euler(0.0f, 0.0f, rotZ - 90.0f);
}

こんな感じですが、Unity的に最もシンプルに書くと

transform.up = target.transform.position - transform.position;

これで済む。覚えておくと楽です。

Posted by kazupon