AngleCounterClockwise() public static method

public static AngleCounterClockwise ( Vector2 a, Vector2 b ) : float
a Vector2
b Vector2
return float
Beispiel #1
0
        /// <summary>
        /// Lerp the current transform rotation to point towards world position "position" using t.
        /// The look-at is planar, meaning that the transform.up will be Vector3.up.
        /// </summary>
        public static void LerpLookAtPlanar(Transform transform, Vector3 position, float t)
        {
            Vector3 targetDir3D = (transform.position - position);

            if (targetDir3D.sqrMagnitude < 0.001f)
            {
                return;
            }

            Vector2 targetDir  = new Vector2(targetDir3D.x, targetDir3D.z);
            Vector2 currentDir = new Vector2(transform.forward.x, transform.forward.z);

            targetDir.Normalize();
            currentDir.Normalize();

            var desiredAngle = MathHelper.AngleCounterClockwise(targetDir, Vector2.down) * Mathf.Rad2Deg;
            var currentAngle = MathHelper.AngleCounterClockwise(currentDir, Vector2.up) * Mathf.Rad2Deg;

            currentAngle = Mathf.LerpAngle(currentAngle, desiredAngle, t);

            transform.rotation = Quaternion.AngleAxis(currentAngle, Vector3.up);
        }