相机下创建CameraShake脚本
using UnityEngine;
using System.Collections;
public class CameraShake: MonoBehaviour {
private Vector3 thisPosition;
private Quaternion thisRotation;
//抖动强度
public float shake_decay;
//衰减速度
public float shake_intensity;
void Start() {
thisPosition = transform.position;
thisRotation = transform.rotation;
}
void Update() {
//点击鼠标右键,实现抖动效果
if (Input.GetMouseButton(1)) Shake();
if (shake_intensity > 0) {
//改变相机的位置
transform.position = thisPosition + Random.insideUnitSphere * shake_intensity;
//改变相机的旋转
transform.rotation = new Quaternion(
thisRotation.x + Random.Range(-shake_intensity, shake_intensity) * .2f,
thisRotation.y + Random.Range(-shake_intensity, shake_intensity) * .2f,
thisRotation.z + Random.Range(-shake_intensity, shake_intensity) * .2f,
thisRotation.w + Random.Range(-shake_intensity, shake_intensity) * .2f);
shake_intensity -= shake_decay;
}
else {
//变回为原来的位置和旋转
transform.position = thisPosition;
transform.rotation = thisRotation;
}
}
void Shake() {
shake_intensity = 0.1f;
shake_decay = 0.001f;
}
}