Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// Corresponding animations for up down left right.
- /// Can select an animation clip based on a facing direction.
- ///
- /// Alternative to blend trees when you don't want a blend tree to blend animation keyframes together but
- /// rather you want the values to be hard set in stone and unchanging even when changing between
- /// different animations from the same set
- /// </summary>
- [CreateAssetMenu(fileName = "DirectionalAnimationSet", menuName = "GathererTopDownRPG/DirectionalAnimationSet")]
- public class DirectionalAnimationSet : ScriptableObject
- {
- [field: SerializeField] public AnimationClip Up { get; private set; }
- [field: SerializeField] public AnimationClip Down { get; private set; }
- [field: SerializeField] public AnimationClip Left { get; private set; }
- [field: SerializeField] public AnimationClip Right { get; private set; }
- /// <summary>
- /// After comparing the expected facing direction to the direction options, return the animation clip
- /// that most closely matches the input facing direction
- /// </summary>
- /// <param name="facingDirection">The directional input for controlling where the character faces</param>
- /// <returns>The animation clip most closely matching the facingDirection</returns>
- /// <exception cref="ArgumentException">Throws when a closest direction is unaccounted for returned by GetClosestDirection</exception>
- public AnimationClip GetFacingClip(Vector2 facingDirection)
- {
- // Get the closest direction to the input
- Vector2 closestDirection = GetClosestDirection(facingDirection);
- // Return the animation clip based on closest direction
- if (closestDirection == Vector2.left)
- {
- return Left;
- }
- else if (closestDirection == Vector2.right)
- {
- return Right;
- }
- else if (closestDirection == Vector2.up)
- {
- return Up;
- }
- else if (closestDirection == Vector2.down)
- {
- return Down;
- }
- else
- {
- throw new ArgumentException($"Direction not expected {closestDirection}");
- }
- }
- /// <summary>
- /// Based on a normalized input direction, return the Vector2 direction in the list of directionsToCheck
- /// that is closest to the input direction by comparing the distance between the input
- /// and all direction Vector2s in the list
- /// </summary>
- /// <param name="inputDirection">The normalized direction to compare to the directionsToCheck</param>
- /// <returns>The closest direction represented as a Vector2</returns>
- public Vector2 GetClosestDirection(Vector2 inputDirection)
- {
- Vector2 normalizedDirection = inputDirection.normalized;
- Vector2 closestDirection = Vector2.zero;
- float closestDistance = 0f;
- bool firstSet = false;
- Vector2[] directionsToCheck = new Vector2[4] { Vector2.down, Vector2.up, Vector2.left, Vector2.right };
- for (int i = 0; i < directionsToCheck.Length; i++)
- {
- if (!firstSet)
- {
- // Set when no comparison to be made yet
- closestDirection = directionsToCheck[i];
- closestDistance = Vector2.Distance(inputDirection, directionsToCheck[i]);
- firstSet = true;
- }
- else
- {
- // Compare to the current closest direction and distance
- float nextDistance = Vector2.Distance(inputDirection, directionsToCheck[i]);
- if (nextDistance < closestDistance)
- {
- // Closer vector found!
- closestDistance = nextDistance;
- closestDirection = directionsToCheck[i];
- }
- }
- }
- return closestDirection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement