特定の方向を向かせる

target = GameObject.Find("Player").transform;

オイラー角を用いる
transform.LookAt(target);

クォータニオンを使う
transform.rotation = Quaternion.LookRotation(target.position - transform.position);
thisから見て、targetの方向を向かせる


特定のObjectの方にスムーズに向く
private void SmoothLookAt(GameObject target)
{
  Vector3 newRotation = Quaternion.LookRotation(target.transform.position-transform.position ).eulerAngles;
  newRotation.x = 0;
  newRotation.z = 0;
  transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(newRotation),
    3f*Time.deltaTime);
}

特定の座標にスムーズに向く
private void SmoothLookAt2(Vector3 target)
{
    Vector3 newRotation = Quaternion.LookRotation(target-transform.position ).eulerAngles;
    newRotation.x = 0;
    newRotation.z = 0;
    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(newRotation),
        3f*Time.deltaTime);
}

タグ:

unity
最終更新:2015年05月26日 19:01