xi/Assets/scripts/ScreenShake.cs
2024-11-27 20:15:02 +08:00

55 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}