AxisTools.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion {
  4. /// <summary>
  5. /// The Cartesian axes.
  6. /// </summary>
  7. [System.Serializable]
  8. public enum Axis {
  9. X,
  10. Y,
  11. Z
  12. }
  13. /// <summary>
  14. /// Contains tools for working with Axes that have no positive/negative directions.
  15. /// </summary>
  16. public class AxisTools {
  17. /// <summary>
  18. /// Converts an Axis to Vector3.
  19. /// </summary>
  20. public static Vector3 ToVector3(Axis axis) {
  21. if (axis == Axis.X) return Vector3.right;
  22. if (axis == Axis.Y) return Vector3.up;
  23. return Vector3.forward;
  24. }
  25. /// <summary>
  26. /// Converts a Vector3 to Axis.
  27. /// </summary>
  28. public static Axis ToAxis(Vector3 v) {
  29. float absX = Mathf.Abs(v.x);
  30. float absY = Mathf.Abs(v.y);
  31. float absZ = Mathf.Abs(v.z);
  32. Axis d = Axis.X;
  33. if (absY > absX && absY > absZ) d = Axis.Y;
  34. if (absZ > absX && absZ > absY) d = Axis.Z;
  35. return d;
  36. }
  37. /// <summary>
  38. /// Returns the Axis of the Transform towards a world space position.
  39. /// </summary>
  40. public static Axis GetAxisToPoint(Transform t, Vector3 worldPosition) {
  41. Vector3 axis = GetAxisVectorToPoint(t, worldPosition);
  42. if (axis == Vector3.right) return Axis.X;
  43. if (axis == Vector3.up) return Axis.Y;
  44. return Axis.Z;
  45. }
  46. /// <summary>
  47. /// Returns the Axis of the Transform towards a world space direction.
  48. /// </summary>
  49. public static Axis GetAxisToDirection(Transform t, Vector3 direction) {
  50. Vector3 axis = GetAxisVectorToDirection(t, direction);
  51. if (axis == Vector3.right) return Axis.X;
  52. if (axis == Vector3.up) return Axis.Y;
  53. return Axis.Z;
  54. }
  55. /// <summary>
  56. /// Returns the local axis of the Transform towards a world space position.
  57. /// </summary>
  58. public static Vector3 GetAxisVectorToPoint(Transform t, Vector3 worldPosition) {
  59. return GetAxisVectorToDirection(t, worldPosition - t.position);
  60. }
  61. /// <summary>
  62. /// Returns the local axis of the Transform that aligns the most with a direction.
  63. /// </summary>
  64. public static Vector3 GetAxisVectorToDirection(Transform t, Vector3 direction) {
  65. return GetAxisVectorToDirection(t.rotation, direction);
  66. }
  67. /// <summary>
  68. /// Returns the local axis of a rotation space that aligns the most with a direction.
  69. /// </summary>
  70. public static Vector3 GetAxisVectorToDirection(Quaternion r, Vector3 direction)
  71. {
  72. direction = direction.normalized;
  73. Vector3 axis = Vector3.right;
  74. float dotX = Mathf.Abs(Vector3.Dot(Vector3.Normalize(r * Vector3.right), direction));
  75. float dotY = Mathf.Abs(Vector3.Dot(Vector3.Normalize(r * Vector3.up), direction));
  76. if (dotY > dotX) axis = Vector3.up;
  77. float dotZ = Mathf.Abs(Vector3.Dot(Vector3.Normalize(r * Vector3.forward), direction));
  78. if (dotZ > dotX && dotZ > dotY) axis = Vector3.forward;
  79. return axis;
  80. }
  81. }
  82. }