Skip to content

Instantly share code, notes, and snippets.

@the6th
Created May 19, 2020 03:52
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 the6th/d25d5ba8527e57814b6e6a6a96228a15 to your computer and use it in GitHub Desktop.
Save the6th/d25d5ba8527e57814b6e6a6a96228a15 to your computer and use it in GitHub Desktop.
World space rotation を local spaceに変換するHelper関数
using UnityEngine;
public static class TransformHelper
{
/// <summary>
/// World座標系のQuaternionを対象のTransformのlocalRotationに変換する
/// </summary>
/// <param name="target">対象のTransform</param>
/// <param name="worldRotation">World座標系のQuaternion</param>
/// <returns>localRotation</returns>
public static Quaternion InverseTransformRotation(this Transform target, Quaternion worldRotation)
{
return Quaternion.Inverse(target.transform.rotation) * worldRotation;
}
/// <summary>
/// World座標系のEulerAnglesを対象のTransformのlocalEulerAnglesに変換する
/// </summary>
/// <param name="target">対象のTransform</param>
/// <param name="worldEuler">World座標系のEulerAngles<</param>
/// <returns>localEulerAngles</returns>
public static Vector3 InverseTransformEulerAngles(this Transform target, Vector3 worldEuler)
{
return target.InverseTransformRotation(Quaternion.Euler(worldEuler)).eulerAngles;
}
}
@the6th
Copy link
Author

the6th commented May 19, 2020

usage

//world space rotation to local space rotation
hoge.transform.InverseTransformRotation(fuga.rotation);

//world eulerAngles to localEulerAngles
hoge.transform.InverseTransformEulerAngles(fuga.eulerAngles);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment