using System.Collections; using System.Collections.Generic; using UnityEngine; namespace UnityLibrary { /// /// Transform extensions. /// Useful transform utilities and methods. /// public static class TransformExtensions { /// /// Rotates the transform so the forward vector points at target's current position. /// /// Transform. /// Target. public static void LookAt2D ( this Transform transform, Transform target ) { transform.LookAt2D ( ( Vector2 )target.position ); } /// /// Rotates the transform so the forward vector points at worldPosition. /// /// Transform. /// World position. public static void LookAt2D ( this Transform transform, Vector3 worldPosition ) { transform.LookAt2D ( ( Vector2 )worldPosition ); } /// /// Rotates the transform so the forward vector points at worldPosition. /// /// Transform. /// World position. public static void LookAt2D ( this Transform transform, Vector2 worldPosition ) { Vector2 distance = worldPosition - ( Vector2 )transform.position; transform.eulerAngles = new Vector3 ( transform.eulerAngles.x, transform.eulerAngles.y, Mathf.Atan2 ( distance.y, distance.x ) * Mathf.Rad2Deg ); } } }