using UnityEngine;
public class ScreenShake : MonoBehaviour
{
float shakeTime = 1.0f;
float fps = 20.0f;
float frameTime = 0.00f;
float shakeDelta = 0.005f;
public Camera cam;
public static bool isshakeCamera = false;
void Start()
{
if (cam == null)
cam = Camera.main;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
isshakeCamera = !isshakeCamera;
}
if (isshakeCamera)
{
if (shakeTime > 0)
{
shakeTime -= Time.deltaTime;
if (shakeTime <= 0)
{
cam.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
isshakeCamera = false;
shakeTime = 1.0f;
fps = 20.0f;
frameTime = 0.03f;
shakeDelta = 0.005f;
}
else
{
frameTime += Time.deltaTime;
if (frameTime > 1.0 / fps)
{
frameTime = 0;
cam.rect = new Rect(shakeDelta * (-1.0f + 2.0f * Random.value), shakeDelta * (-1.0f + 2.0f * Random.value), 1.0f, 1.0f);
}
}
}
}
}
}
Unity 屏幕震动效果实现
最新推荐文章于 2024-06-16 15:13:33 发布