fahimkamal63

SquashNStretch

Jul 30th, 2024
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.99 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Random = UnityEngine.Random;
  6.  
  7. public class SquashNStretch : MonoBehaviour
  8. {
  9.     [Header("Notes")]
  10.     [SerializeField, Multiline(2)] private string notes;
  11.  
  12.     [Header("Squash and stretch Core")]
  13.     [SerializeField] [Tooltip("Defaults to current Go if not set and Should be child transform")]
  14.     private Transform transformToAffect;
  15.     [SerializeField] private SquashStretchAxis axisToAffect = SquashStretchAxis.Y;
  16.    
  17.     [Flags]
  18.     public enum SquashStretchAxis
  19.     {
  20.         None = 0,
  21.         X = 1,
  22.         Y = 2,
  23.         Z = 4,
  24.     }
  25.     [SerializeField, Range(0, 1f)] private float animationDuration = 0.25f;
  26.     [SerializeField] private bool canBeOverWritten;
  27.     [SerializeField] private bool playOnStart;
  28.     [SerializeField] private bool playsEveryTime = true;
  29.     [SerializeField, Range(0, 100f), Tooltip("% from 0 to 100%")]
  30.     private float chanceToPlay = 100f;
  31.    
  32.  
  33.     [Header("Animation Settings")]
  34.     [SerializeField] private float initialScale = 1f;
  35.     [SerializeField] private float maximumScale = 1.3f;
  36.     [SerializeField] private bool resetToInitialScaleAfterAnimation = true;
  37.     [SerializeField] private bool reverseAnimationCurveAfterPlaying;
  38.     private bool _isReversed;
  39.    
  40.     [SerializeField] private AnimationCurve squashAndStretchCurve = new AnimationCurve(
  41.         new Keyframe(0f, 0f),
  42.         new Keyframe(0.25f, 1f),
  43.         new Keyframe(1f, 0f)
  44.         );
  45.    
  46.     [Header("Looping Settings")]
  47.     [SerializeField] private bool looping;
  48.     [SerializeField] private float loopingDelay = 0.5f;
  49.    
  50.     private Coroutine _squashAndStretchCoroutine;
  51.     private WaitForSeconds _loopingDelayWaitForSeconds;
  52.     private Vector3 _initialScaleVector;
  53.    
  54.     private bool affectX => (axisToAffect & SquashStretchAxis.X) != 0;
  55.     private bool affectY => (axisToAffect & SquashStretchAxis.Y) != 0;
  56.     private bool affectZ => (axisToAffect & SquashStretchAxis.Z) != 0;
  57.     // Will Add Later
  58.  
  59.     private void Awake()
  60.     {
  61.         if (transformToAffect == null)
  62.         {
  63.             transformToAffect = transform;
  64.         }
  65.         _initialScaleVector = transformToAffect.localScale;
  66.         _loopingDelayWaitForSeconds = new WaitForSeconds(loopingDelay);
  67.     }
  68.  
  69.     private void Start()
  70.     {
  71.         if (playOnStart)
  72.         {
  73.             CheckForAndStartCoroutine();
  74.         }
  75.     }
  76.  
  77.    
  78.     [ContextMenu("Play Squash and Stretch")]
  79.     public void PlaySquashAndStretch()
  80.     {
  81.         if (looping && !canBeOverWritten)
  82.         {
  83.             return;
  84.         }
  85.         CheckForAndStartCoroutine();
  86.     }
  87.  
  88.     private void CheckForAndStartCoroutine()
  89.     {
  90.         if (axisToAffect == SquashStretchAxis.None)
  91.         {
  92.             Debug.LogWarning("Axis to affect is set to None", gameObject);
  93.             return;
  94.         }
  95.  
  96.         if (_squashAndStretchCoroutine != null)
  97.         {
  98.             Debug.Log("working");
  99.             StopCoroutine(_squashAndStretchCoroutine);
  100.             if (playsEveryTime && resetToInitialScaleAfterAnimation)
  101.             {
  102.                 transform.localScale = _initialScaleVector;
  103.             }
  104.         }
  105.  
  106.         _squashAndStretchCoroutine = StartCoroutine(SquashAndStretchEffect());
  107.     }
  108.  
  109.     private IEnumerator SquashAndStretchEffect()
  110.     {
  111.         Debug.Log("Squash And Stretch");
  112.         do
  113.         {
  114.             if (!playsEveryTime)
  115.             {
  116.                 var random = Random.Range(0, 100f);
  117.                 if (random > chanceToPlay)
  118.                 {
  119.                     yield return null;
  120.                     continue;
  121.                 }
  122.             }
  123.            
  124.             if (reverseAnimationCurveAfterPlaying)
  125.             {
  126.                 _isReversed = !_isReversed;
  127.             }
  128.            
  129.             var elapsedTime = 0f;
  130.             var originalScale = _initialScaleVector;
  131.             var modifiedScale = originalScale;
  132.  
  133.             while (elapsedTime < animationDuration)
  134.             {
  135.                 Debug.Log(elapsedTime + " / " + animationDuration);
  136.                 elapsedTime += Time.deltaTime;
  137.  
  138.                 float curvePosition;
  139.                 if (_isReversed)
  140.                 {
  141.                     curvePosition = 1 - (elapsedTime / animationDuration);
  142.                 }
  143.                 else
  144.                 {
  145.                     curvePosition = elapsedTime / animationDuration;
  146.                 }
  147.                 var curveValue = squashAndStretchCurve.Evaluate(curvePosition);
  148.                 var remappedValue = initialScale + (curveValue * (maximumScale - initialScale));
  149.  
  150.                 var minimumThreshold = 0.0001f;
  151.                 if (Mathf.Abs(remappedValue) < minimumThreshold)
  152.                 {
  153.                     remappedValue = minimumThreshold;
  154.                 }
  155.  
  156.                 if (affectX)
  157.                     modifiedScale.x = originalScale.x * remappedValue;
  158.                 else
  159.                     modifiedScale.x = originalScale.x / remappedValue;
  160.  
  161.                 if (affectY)
  162.                     modifiedScale.y = originalScale.y * remappedValue;
  163.                 else
  164.                     modifiedScale.y = originalScale.y / remappedValue;
  165.  
  166.                 if (affectZ)
  167.                     modifiedScale.z = originalScale.z * remappedValue;
  168.                 else
  169.                     modifiedScale.z = originalScale.z / remappedValue;
  170.                
  171.                 transformToAffect.localScale = modifiedScale;
  172.                
  173.                 yield return new WaitForSeconds(0.03f);
  174.             }
  175.  
  176.             if (resetToInitialScaleAfterAnimation)
  177.             {
  178.                 transformToAffect.localScale = originalScale;
  179.             }
  180.  
  181.             if (looping)
  182.             {
  183.                 yield return _loopingDelayWaitForSeconds;
  184.             }
  185.            
  186.         } while (looping);
  187.     }
  188.  
  189.     public void SetLooping(bool shouldLoop)
  190.     {
  191.         looping = shouldLoop;
  192.     }
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment