Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Original script by Joyce[MinionsArt]
- using UnityEngine;
- public class LiquidWobble : MonoBehaviour {
- [SerializeField]
- private Renderer _targetRenderer;
- public Renderer TargetRenderer {
- get { return _targetRenderer; }
- set { _targetRenderer = value; }
- }
- [Header("Wobble")]
- [SerializeField]
- private float _maxWobble;
- public float MaxWobble {
- get { return _maxWobble; }
- set { _maxWobble = value; }
- }
- [SerializeField]
- private float _wobbleSpeed;
- public float WobbleSpeed {
- get { return _wobbleSpeed; }
- set { _wobbleSpeed = value; }
- }
- [SerializeField]
- private float _recoverySpeed;
- public float RecoverySpeed {
- get { return _recoverySpeed; }
- set { _recoverySpeed = value; }
- }
- private float _wobbleAmountX;
- private float _wobbleAmountZ;
- private float _wobbleAmountToAddX;
- private float _wobbleAmountToAddZ;
- private float _pulse;
- private float _currentTime = 0.5f;
- private Vector3 _lastPosition;
- private Vector3 _velocity;
- private void Update () {
- if (_targetRenderer) {
- _currentTime += Time.deltaTime;
- // Decrease wobble over time
- _wobbleAmountToAddX = Mathf.Lerp(_wobbleAmountToAddX, 0f, Time.deltaTime * (RecoverySpeed));
- _wobbleAmountToAddZ = Mathf.Lerp(_wobbleAmountToAddZ, 0f, Time.deltaTime * (RecoverySpeed));
- // Make a sine wave of the decreasing wobble
- _pulse = 2f * Mathf.PI * WobbleSpeed;
- _wobbleAmountX = _wobbleAmountToAddX * Mathf.Sin(_pulse * _currentTime);
- _wobbleAmountZ = _wobbleAmountToAddZ * Mathf.Sin(_pulse * _currentTime);
- // Send it to the shader
- _targetRenderer.material.SetFloat("_WobbleX", _wobbleAmountX);
- _targetRenderer.material.SetFloat("_WobbleZ", _wobbleAmountZ);
- // Calculate velocity
- _velocity = (_lastPosition - transform.position) / Time.deltaTime;
- // Add clamped velocity to wobble
- _wobbleAmountToAddX += Mathf.Clamp(_velocity.x * MaxWobble, -MaxWobble, MaxWobble);
- _wobbleAmountToAddZ += Mathf.Clamp(_velocity.z * MaxWobble, -MaxWobble, MaxWobble);
- // Remember position for next frame
- _lastPosition = transform.position;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment