一:简单版
当按下空格按键时,屏幕开始震动(将脚本挂载到相机上)
using UnityEngine;
public class Shake : MonoBehaviour
{
private bool isShake;//是否震动
private float currentShakeValue;//当前震动系数
public float shakeValue = 2;//震动系数
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
isShake = true;
}
if (isShake)
{
//先左右震动再上下震动
transform.position = new Vector3((Random.Range(-currentShakeValue, currentShakeValue)), transform.position .y, transform.position .z);
transform.position = new Vector3(transform.position .x, (Random.Range(-currentShakeValue, currentShakeValue)), transform.position .z);
currentShakeValue /= 1.05f;//震动数值减少