57 lines
2.1 KiB
C#
57 lines
2.1 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace BNG {
|
|||
|
|
|||
|
// This is a wobble effect based on Minions Art Tutorial found : https://www.patreon.com/posts/quick-game-art-18245226
|
|||
|
public class LiquidWobble : MonoBehaviour {
|
|||
|
Renderer rend;
|
|||
|
Vector3 lastPos;
|
|||
|
Vector3 velocity;
|
|||
|
Vector3 lastRot;
|
|||
|
Vector3 angularVelocity;
|
|||
|
public float MaxWobble = 0.03f;
|
|||
|
public float WobbleSpeed = 1f;
|
|||
|
public float Recovery = 1f;
|
|||
|
float wobbleAmountX;
|
|||
|
float wobbleAmountZ;
|
|||
|
float wobbleAmountToAddX;
|
|||
|
float wobbleAmountToAddZ;
|
|||
|
float pulse;
|
|||
|
float time = 0.5f;
|
|||
|
|
|||
|
void Start() {
|
|||
|
rend = GetComponent<Renderer>();
|
|||
|
}
|
|||
|
private void Update() {
|
|||
|
time += Time.deltaTime;
|
|||
|
// decrease wobble over time
|
|||
|
wobbleAmountToAddX = Mathf.Lerp(wobbleAmountToAddX, 0, Time.deltaTime * (Recovery));
|
|||
|
wobbleAmountToAddZ = Mathf.Lerp(wobbleAmountToAddZ, 0, Time.deltaTime * (Recovery));
|
|||
|
|
|||
|
// make a sine wave of the decreasing wobble
|
|||
|
pulse = 2 * Mathf.PI * WobbleSpeed;
|
|||
|
wobbleAmountX = wobbleAmountToAddX * Mathf.Sin(pulse * time);
|
|||
|
wobbleAmountZ = wobbleAmountToAddZ * Mathf.Sin(pulse * time);
|
|||
|
|
|||
|
// send it to the shader
|
|||
|
rend.material.SetFloat("_WobbleX", wobbleAmountX);
|
|||
|
rend.material.SetFloat("_WobbleZ", wobbleAmountZ);
|
|||
|
|
|||
|
// velocity
|
|||
|
velocity = (lastPos - transform.position) / Time.deltaTime;
|
|||
|
angularVelocity = transform.rotation.eulerAngles - lastRot;
|
|||
|
|
|||
|
|
|||
|
// add clamped velocity to wobble
|
|||
|
wobbleAmountToAddX += Mathf.Clamp((velocity.x + (angularVelocity.z * 0.2f)) * MaxWobble, -MaxWobble, MaxWobble);
|
|||
|
wobbleAmountToAddZ += Mathf.Clamp((velocity.z + (angularVelocity.x * 0.2f)) * MaxWobble, -MaxWobble, MaxWobble);
|
|||
|
|
|||
|
// keep last position
|
|||
|
lastPos = transform.position;
|
|||
|
lastRot = transform.rotation.eulerAngles;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|