Advertisement
ChrisTutorials

DirectionalAnimationSet from GathererTopDown Tutorial Series

Mar 5th, 2023
1,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. /// <summary>
  7. /// Corresponding animations for up down left right.
  8. /// Can select an animation clip based on a facing direction.
  9. ///
  10. /// Alternative to blend trees when you don't want a blend tree to blend animation keyframes together but
  11. /// rather you want the values to be hard set in stone and unchanging even when changing between
  12. /// different animations from the same set
  13. /// </summary>
  14. [CreateAssetMenu(fileName = "DirectionalAnimationSet", menuName = "GathererTopDownRPG/DirectionalAnimationSet")]
  15. public class DirectionalAnimationSet : ScriptableObject
  16. {
  17.     [field: SerializeField] public AnimationClip Up { get; private set; }
  18.     [field: SerializeField] public AnimationClip Down { get; private set; }
  19.     [field: SerializeField] public AnimationClip Left { get; private set; }
  20.     [field: SerializeField] public AnimationClip Right { get; private set; }
  21.  
  22.     /// <summary>
  23.     /// After comparing the expected facing direction to the direction options, return the animation clip
  24.     /// that most closely matches the input facing direction
  25.     /// </summary>
  26.     /// <param name="facingDirection">The directional input for controlling where the character faces</param>
  27.     /// <returns>The animation clip most closely matching the facingDirection</returns>
  28.     /// <exception cref="ArgumentException">Throws when a closest direction is unaccounted for returned by GetClosestDirection</exception>
  29.     public AnimationClip GetFacingClip(Vector2 facingDirection)
  30.     {
  31.         // Get the closest direction to the input
  32.         Vector2 closestDirection = GetClosestDirection(facingDirection);
  33.  
  34.         // Return the animation clip based on closest direction
  35.         if (closestDirection == Vector2.left)
  36.         {
  37.             return Left;
  38.         }
  39.         else if (closestDirection == Vector2.right)
  40.         {
  41.             return Right;
  42.         }
  43.         else if (closestDirection == Vector2.up)
  44.         {
  45.             return Up;
  46.         }
  47.         else if (closestDirection == Vector2.down)
  48.         {
  49.             return Down;
  50.         }
  51.         else
  52.         {
  53.             throw new ArgumentException($"Direction not expected {closestDirection}");
  54.         }
  55.     }
  56.  
  57.     /// <summary>
  58.     /// Based on a normalized input direction, return the Vector2 direction in the list of directionsToCheck
  59.     /// that is closest to the input direction by comparing the distance between the input
  60.     /// and all direction Vector2s in the list
  61.     /// </summary>
  62.     /// <param name="inputDirection">The normalized direction to compare to the directionsToCheck</param>
  63.     /// <returns>The closest direction represented as a Vector2</returns>
  64.     public Vector2 GetClosestDirection(Vector2 inputDirection)
  65.     {
  66.         Vector2 normalizedDirection = inputDirection.normalized;
  67.  
  68.         Vector2 closestDirection = Vector2.zero;
  69.         float closestDistance = 0f;
  70.         bool firstSet = false;
  71.  
  72.         Vector2[] directionsToCheck = new Vector2[4] { Vector2.down, Vector2.up, Vector2.left, Vector2.right };
  73.  
  74.         for (int i = 0; i < directionsToCheck.Length; i++)
  75.         {
  76.             if (!firstSet)
  77.             {
  78.                 // Set when no comparison to be made yet
  79.                 closestDirection = directionsToCheck[i];
  80.                 closestDistance = Vector2.Distance(inputDirection, directionsToCheck[i]);
  81.                 firstSet = true;
  82.             }
  83.             else
  84.             {
  85.                 // Compare to the current closest direction and distance
  86.                 float nextDistance = Vector2.Distance(inputDirection, directionsToCheck[i]);
  87.  
  88.                 if (nextDistance < closestDistance)
  89.                 {
  90.                     // Closer vector found!
  91.                     closestDistance = nextDistance;
  92.                     closestDirection = directionsToCheck[i];
  93.                 }
  94.             }
  95.         }
  96.  
  97.         return closestDirection;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement