OhiraKyou

Liquid wobble cleanup

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