Skip to content

Instantly share code, notes, and snippets.

@GOROman
Created September 10, 2017 05:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GOROman/d3ae57ed431e59c341ad6593760ce6c5 to your computer and use it in GitHub Desktop.
Save GOROman/d3ae57ed431e59c341ad6593760ce6c5 to your computer and use it in GitHub Desktop.
GOROman's Look At Test
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