OhiraKyou

Liquid wobble V2

Apr 17th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. // Original script by Joyce[MinionsArt]
  2.  
  3. using OhiraKyou;
  4. using UnityEngine;
  5.  
  6. public class LiquidWobble : MonoBehaviour {
  7.  
  8.     [Header("Setup")]
  9.     [SerializeField]
  10.     private Transform _velocityReferenceTransform;
  11.     public Transform VelocityReferenceTransform {
  12.         get { return _velocityReferenceTransform; }
  13.         set { _velocityReferenceTransform = value; }
  14.     }
  15.  
  16.     [SerializeField]
  17.     private Renderer _targetRenderer;
  18.     public Renderer TargetRenderer {
  19.         get { return _targetRenderer; }
  20.         set { _targetRenderer = value; }
  21.     }
  22.  
  23.     [Header("Wobble")]
  24.     [SerializeField]
  25.     private float _maxWobble = 0.05f;
  26.     public float MaxWobble {
  27.         get { return _maxWobble; }
  28.         set { _maxWobble = value; }
  29.     }
  30.  
  31.     [SerializeField]
  32.     private float _wobbleDuration = 1.5f;
  33.     public float WobbleDuration {
  34.         get { return _wobbleDuration; }
  35.         set { _wobbleDuration = value; }
  36.     }
  37.  
  38.     [SerializeField]
  39.     private float _recoverySpeed = 1f;
  40.     public float RecoverySpeed {
  41.         get { return _recoverySpeed; }
  42.         set { _recoverySpeed = value; }
  43.     }
  44.  
  45.     private float _wobbleAmountX;
  46.     private float _wobbleAmountZ;
  47.     private float _wobbleAmountToAddX;
  48.     private float _wobbleAmountToAddZ;
  49.     private float _pulse;
  50.     private float _currentTime = 0.5f;
  51.     private Vector3 _lastPosition;
  52.     private Vector3 _velocity;
  53.  
  54.  
  55.     private void Update () {
  56.         if (_targetRenderer) {
  57.  
  58.             // Decrease wobble over time
  59.             _wobbleAmountToAddX = Mathf.Lerp(_wobbleAmountToAddX, 0f, Time.deltaTime * RecoverySpeed);
  60.             _wobbleAmountToAddZ = Mathf.Lerp(_wobbleAmountToAddZ, 0f, Time.deltaTime * RecoverySpeed);
  61.  
  62.             // Calculate the current wobble time percent
  63.             _currentTime = CustomMath.GetWrappedSeconds(_currentTime, WobbleDuration);
  64.             float wobblePercent = _currentTime / WobbleDuration;
  65.  
  66.             // The result of 0 and 360 degrees should be identical, and there are 2pi radians in 360 degrees
  67.             float wobbleRadians = wobblePercent * 2f * Mathf.PI;
  68.  
  69.             // Make a sine wave of the decreasing wobble
  70.             _wobbleAmountX = _wobbleAmountToAddX * Mathf.Sin(wobbleRadians);
  71.             _wobbleAmountZ = _wobbleAmountToAddZ * Mathf.Sin(wobbleRadians);
  72.  
  73.             // Send it to the shader
  74.             _targetRenderer.material.SetFloat("_WobbleX", _wobbleAmountX);
  75.             _targetRenderer.material.SetFloat("_WobbleZ", _wobbleAmountZ);
  76.  
  77.  
  78.             // Calculate velocity
  79.             _velocity = (_lastPosition - _velocityReferenceTransform.position) / Time.deltaTime;
  80.  
  81.             // Add clamped velocity to wobble
  82.             _wobbleAmountToAddX += Mathf.Clamp(_velocity.x * MaxWobble, -MaxWobble, MaxWobble);
  83.             _wobbleAmountToAddZ += Mathf.Clamp(_velocity.z * MaxWobble, -MaxWobble, MaxWobble);
  84.  
  85.             // Remember position for next frame
  86.             _lastPosition = _velocityReferenceTransform.position;
  87.  
  88.         }
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment