WinterJamSnowman/Assets/Scripts/LemonGenericLib/Visual Effects/SpriteVibration.cs

47 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Lemon.GenericLib.VFX
{
public class SpriteVibration : MonoBehaviour
{
[SerializeField] Vector2 vibrationRange;
private Vector2 originalPos;
private bool isVibrating = false;
// Start is called before the first frame update
void Start()
{
originalPos = transform.localPosition;
StartCoroutine(Viberate());
}
IEnumerator Viberate()
{
while (true)
{
if (isVibrating)
transform.localPosition = originalPos + new Vector2(Random.Range(-vibrationRange.x, vibrationRange.x), Random.Range(-vibrationRange.y, vibrationRange.y));
//else transform.localPosition = originalPos;
yield return null;
}
}
public void SetVibration(bool value) {
isVibrating = value;
if (!isVibrating) transform.localPosition = originalPos;
}
public void SetTempVibration(float duration) {
StartCoroutine(SetTempVibrationRoutine(duration));
}
IEnumerator SetTempVibrationRoutine(float duration) {
SetVibration(true);
yield return new WaitForSeconds(duration);
SetVibration(false);
}
}
}