55 lines
1.3 KiB
C#
55 lines
1.3 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using DG.Tweening;
|
||
public class ScreenShake : MonoBehaviour
|
||
{
|
||
//public float shakeDuration = 0.5f;
|
||
//public float shakeMagnitude = 0.1f;
|
||
|
||
//void Update()
|
||
//{
|
||
// if (Input.GetKeyDown(KeyCode.S))
|
||
// {
|
||
// // 使摄像机在0.5秒内产生位置震动,幅度为0.1f
|
||
// Camera.main.DOShakePosition(shakeDuration, shakeMagnitude);
|
||
// }
|
||
//}
|
||
|
||
public float shakeDuration = 0.5f; // 震动持续时间
|
||
public float shakeMagnitude = 1f; // 震动幅度
|
||
private Vector3 originalPosition; // 摄像机原始位置
|
||
private float shakeElapsedTime = 0f;
|
||
|
||
void Start()
|
||
{
|
||
originalPosition = transform.position;
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
if (shakeElapsedTime > 0)
|
||
{
|
||
// 生成随机偏移量来模拟抖动
|
||
Vector3 randomOffset = Random.insideUnitSphere * shakeMagnitude;
|
||
transform.position = originalPosition + randomOffset;
|
||
|
||
shakeElapsedTime -= Time.deltaTime;
|
||
}
|
||
else
|
||
{
|
||
// 震动结束,恢复摄像机原始位置
|
||
transform.position = originalPosition;
|
||
}
|
||
if (Input.GetKeyDown(KeyCode.E))
|
||
{
|
||
Shake();
|
||
}
|
||
}
|
||
|
||
public void Shake()
|
||
{
|
||
shakeElapsedTime = shakeDuration;
|
||
}
|
||
}
|