123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Wobble : 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;
- // Start is called before the first frame update
- void Start()
- {
- rend = GetComponent<Renderer>();
- }
- // Update is called once per frame
- 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 = (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);
- lastpos = transform.position;
- lastRot = transform.rotation.eulerAngles;
- }
- }
|