using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShack : MonoBehaviour
{
public Vector3 originalPosition; //摄像机原始本地位置
// Start is called before the first frame update
void Start()
{
//记录摄像机原始本地位置
originalPosition = transform.localPosition;
}
//参1 震动时长
//参2 震动强度
public void Shake(float duration, float intensity)
{
StartCoroutine(ShakeCoroutine(duration,intensity));
}
private IEnumerator ShakeCoroutine(float duration, float intensity)
{
float elapsedTime = 0f;
//再指定的持续时间内执行摄像机抖动效果
while (elapsedTime<duration)
{
//生成一个随机偏移量
//Random.insideUnitSphere 再一个半径为1的圆里随机一个坐标
Vector3 shakeoffset = Random.insideUnitSphere * intensity;
//将摄像机本地位置设置为原始位置加上偏移量
transform.localPosition = originalPosition + shakeoffset;
//等待一帧
yield return null;
//更新已经过时间
elapsedTime += Time.deltaTime;
}
yield return null;
}
}