Files
2025-01-25 04:38:09 +08:00

67 lines
1.7 KiB
C#

using UnityEngine;
using System.Collections;
namespace Thousandto.Cinematic
{
/// <summary>
/// 振动效果
/// </summary>
public class CinematicShake : MonoBehaviour
{
//持续时间
private float _duration = 2f;
//振动强度,值越大,频率越小
private float _strenth = 3f;
//每一帧位置偏移
private Vector3 _deltaPos = Vector3.zero;
//保存之前的位置
private Vector3 _oldPosition;
//开始振动标记
private bool _startShake = false;
// Use this for initialization
void Start()
{
}
public void StartShake(float duration = 2f, float strenth = 3f)
{
_duration = duration;
_strenth = strenth;
if (strenth <= 0)
_strenth = 0.1f;
_oldPosition = transform.localPosition;
_deltaPos = Vector3.zero;
_startShake = true;
this.enabled = true;
}
private void OnShakeEnd()
{
transform.localPosition = _oldPosition;
this.enabled = false;
}
// Update is called once per frame
void Update()
{
if (!_startShake) return;
if (_duration > 0)
{
_duration -= Time.deltaTime;
transform.localPosition -= _deltaPos;
_deltaPos = UnityEngine.Random.insideUnitSphere / _strenth;
transform.localPosition += _deltaPos;
}
else
{
_duration = 0;
_startShake = false;
OnShakeEnd();
}
}
}
}