Skip to content

Instantly share code, notes, and snippets.

@gamebox777
Last active June 27, 2020 15:06
Show Gist options
  • Select an option

  • Save gamebox777/ffd57175e581ce40887fd53462f83d32 to your computer and use it in GitHub Desktop.

Select an option

Save gamebox777/ffd57175e581ce40887fd53462f83d32 to your computer and use it in GitHub Desktop.
Quaternion超入門2
using UnityEngine;
using TMPro;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Quaternion02
{
/// <summary>
/// 任意の回転軸に沿って回転させる
/// </summary>
public class Quaternion02 : MonoBehaviour
{
/// <summary> 回転させるモデル </summary>
public GameObject mModel;
/// <summary> 回転スピード </summary>
public float mRotSpeed = 1f;
/// <summary> テキスト </summary>
public TMP_Text mTextRot;
/// <summary> 回転軸 </summary>
public Vector3 mAxisVector;
/// <summary> 回転軸変更検知用 </summary>
private Vector3 mOldAxisVector = new Vector3(-1,-1,-1);
// Update is called once per frame
void Update()
{
//回転軸が前フレと変わった時は姿勢を初期化
if (mOldAxisVector != mAxisVector)
{
mModel.transform.LookAt(mAxisVector.normalized, Vector3.up);
mOldAxisVector = mAxisVector;
}
//元の角度
var rot = mModel.transform.rotation;
//選択中の軸を元に回転させる回転量
var add = Quaternion.AngleAxis( mRotSpeed, mAxisVector.normalized );
//角度を合成 todo:前回ブログ記事とかける順番を逆にした
mModel.transform.rotation = add * rot;
//オブジェクトのRotation値(eular)表示
mTextRot.text = "mAxisVector:" + mAxisVector.ToString() +
"\nModelRotation(euler):" + rot.eulerAngles.ToString();
}
/// <summary>
/// 回転軸を表示
/// </summary>
void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawLine(-mAxisVector.normalized, mAxisVector.normalized);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment