|
| 1 | +using UnityEngine; |
| 2 | + |
| 3 | +public class flipOnMove : MonoBehaviour |
| 4 | +{ |
| 5 | + // The SpriteRenderer component to flip. Auto-assigned if not set |
| 6 | + [Header("References")] |
| 7 | + [SerializeField] private SpriteRenderer spriteRenderer; |
| 8 | + |
| 9 | + [Header("Flip Settings")] |
| 10 | + // If true, uses flipX. If false, uses flipY |
| 11 | + [SerializeField] private bool useFlipX = true; |
| 12 | + |
| 13 | + // Enable this if your sprite is exported facing left by default |
| 14 | + // This will invert the flip logic |
| 15 | + [SerializeField] private bool invertFlip = false; |
| 16 | + |
| 17 | + // Minimum movement speed required to trigger a flip |
| 18 | + // Prevents flickering when the object is nearly stationary |
| 19 | + [SerializeField] private float flipThreshold = 0.01f; |
| 20 | + |
| 21 | + [Header("Movement Source")] |
| 22 | + // Optional Rigidbody2D for velocity-based detection |
| 23 | + // If null, falls back to position tracking |
| 24 | + [SerializeField] private Rigidbody2D rb; |
| 25 | + |
| 26 | + // Internal reference to the transform (cached for performance) |
| 27 | + private Transform targetTransform; |
| 28 | + |
| 29 | + // Stores the last frame's position for position-based movement detection |
| 30 | + private Vector3 lastPosition; |
| 31 | + |
| 32 | + // Awake is called when the script instance is being loaded |
| 33 | + private void Awake() |
| 34 | + { |
| 35 | + // Try to get SpriteRenderer from this GameObject if not assigned |
| 36 | + if (spriteRenderer == null) |
| 37 | + spriteRenderer = GetComponent<SpriteRenderer>(); |
| 38 | + |
| 39 | + // Try to get Rigidbody2D from this GameObject if not assigned |
| 40 | + if (rb == null) |
| 41 | + rb = GetComponent<Rigidbody2D>(); |
| 42 | + |
| 43 | + // Cache the transform reference for better performance |
| 44 | + targetTransform = transform; |
| 45 | + |
| 46 | + // Initialize last position to current position |
| 47 | + lastPosition = targetTransform.position; |
| 48 | + } |
| 49 | + |
| 50 | + // LateUpdate is called after all Update functions have been called |
| 51 | + // This ensures movement has been processed before we check direction |
| 52 | + private void LateUpdate() |
| 53 | + { |
| 54 | + // Get the horizontal movement direction (-1 for left, +1 for right) |
| 55 | + float direction = GetMovementDirection(); |
| 56 | + |
| 57 | + // Only flip if movement exceeds the threshold |
| 58 | + // This prevents unwanted flipping when idle or barely moving |
| 59 | + if (Mathf.Abs(direction) > flipThreshold) |
| 60 | + { |
| 61 | + ApplyFlip(direction); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Determines the horizontal movement direction |
| 66 | + // Returns: Negative for left movement, positive for right movement |
| 67 | + private float GetMovementDirection() |
| 68 | + { |
| 69 | + // If a Rigidbody2D is attached, use its velocity (more accurate for physics objects) |
| 70 | + if (rb != null) |
| 71 | + { |
| 72 | + return rb.velocity.x; |
| 73 | + } |
| 74 | + // Otherwise, calculate direction from position change (works for non-physics movement) |
| 75 | + else |
| 76 | + { |
| 77 | + float direction = targetTransform.position.x - lastPosition.x; |
| 78 | + lastPosition = targetTransform.position; |
| 79 | + return direction; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Applies the flip to the sprite based on movement direction |
| 84 | + // Parameters: |
| 85 | + // direction - The horizontal movement direction (negative = left, positive = right) |
| 86 | + private void ApplyFlip(float direction) |
| 87 | + { |
| 88 | + // By default, flip when moving left (direction < 0) |
| 89 | + bool shouldFlip = direction < 0; |
| 90 | + |
| 91 | + // If invertFlip is enabled, reverse the flip logic |
| 92 | + // Useful for sprites that are drawn facing left by default |
| 93 | + if (invertFlip) |
| 94 | + shouldFlip = !shouldFlip; |
| 95 | + |
| 96 | + // Apply the flip to the selected axis |
| 97 | + if (useFlipX) |
| 98 | + { |
| 99 | + spriteRenderer.flipX = shouldFlip; |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + spriteRenderer.flipY = shouldFlip; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments