Created
September 10, 2017 05:12
-
-
Save GOROman/d3ae57ed431e59c341ad6593760ce6c5 to your computer and use it in GitHub Desktop.
GOROman's Look At Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class GOROmanLookAt : MonoBehaviour { | |
public Transform target; // ターゲット位置 | |
public Transform upVectorTarget; // アップベクター用のターゲット位置 | |
public float swivelAngle = 0.0f; // あとからクルクルする用の回転角 | |
public bool buildInLookAt = true; // 内蔵のLookAtを使うか? | |
void Update () { | |
// ターゲット位置からアップベクターを求める | |
Vector3 upVector = (upVectorTarget.position - transform.position).normalized; | |
if (buildInLookAt) | |
{ | |
// LookAtを使う | |
transform.LookAt(target.position, upVector); | |
} | |
else | |
{ | |
// 自分をターゲットの向きに向ける(Look At相当) | |
Vector3 direction = (target.position - transform.position); | |
transform.rotation = Quaternion.LookRotation(direction, upVector); | |
} | |
// あとからクルクルする用 | |
transform.rotation = Quaternion.AxisAngle(transform.forward, Mathf.Deg2Rad * swivelAngle) * transform.rotation; | |
// デバッグ用 | |
Debug.DrawLine(transform.position, target.position, Color.blue, 0.05f, false); | |
Debug.DrawLine(transform.position, upVectorTarget.position, Color.green, 0.05f, false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment