本文实例为大家分享了unity3D实现摄像机抖动的具体代码,供大家参考,具体内容如下
摄像机抖动特效 在需要的地方调用CameraShake.Shake()方法就可以
public class CameraShake : MonoBehaviour
{
///
/// The cameras to shake.
///
public List cameras = new List();
///
/// The maximum number of shakes to perform.
///
public int numberOfShakes = 2;
///
/// The amount to shake in each direction.
///
public Vector3 shakeAmount = Vector3.one;
///
/// The amount to rotate in each direction.
///
public Vector3 rotationAmount = Vector3.one;
///
/// The initial distance for the first shake.
///
public float distance = 00.10f;
///
/// The speed multiplier for the shake.
///
public float speed = 50.00f;
///
/// The decay speed (between 0 and 1). Higher values will stop shaking sooner.
///
public float decay = 00.20f;
///
/// The modifier applied to speed in order to shake the GUI.
///
public float guiShakeModifier = 01.00f;
///
/// If true, multiplies the final shake speed by the time scale.
///
public bool multiplyByTimeScale = true;
// Shake rect (for GUI)
private Rect shakeRect;
// States
private bool shaking = false;
private bool cancelling = false;
internal class ShakeState
{
internal readonly Vector3 startPosition;
internal readonly Quaternion startRotation;
internal readonly Vector2 guiStartPosition;
internal Vector3 shakePosition;
internal Quaternion shakeRotation;
internal Vector2 guiShakePosition;
internal ShakeState(Vector3 position, Quaternion rotation, Vector2 guiPosition)
{
startPosition = position;
startRotation = rotation;
guiStartPosition = guiPosition;
shakePosition = position;
shakeRotation = rotation;
guiShakePosition = guiPosition;
}
}
private Dictionary> states = new Dictionary>();
private Dictionary shakeCount = new Dictionary();
// Minimum shake values
private const bool checkForMinimumValues = true;
private const float minShakeValue = 0.001f;
private const float minRotationValue = 0.001f;
#region Singleton
///
/// The Camera Shake singleton instance.
///
public static CameraShake instance;
private void OnEnable()
{
if (cameras.Count < 1)
{
if (camera)
cameras.Add(camera);
}
if (cameras.Count < 1)
{
if (Camera.main)
cameras.Add(Camera.main);
}
if (cameras.Count < 1)
{
Debug.LogError("Camera Shake: No cameras assigned in the inspector!");
}
instance = this;
}
#endregion
#region Static properties
public static bool isShaking
{
get
{
return instance.IsShaking();
}
}
public static bool isCancelling
{
get
{
return instance.IsCancelling();
}
}
#endregion
#region Static methods
public static void Shake()
{
instance.DoShake();
}
public static void Shake(int numberOfShakes, Vector3 shakeAmount, Vector3 rotationAmount, float distance, float speed, float decay, float guiShakeModifier, bool multiplyByTimeScale)
{
instance.DoShake(numberOfShakes, shakeAmount, rotationAmount, distance, speed, decay, guiShakeModifier, multiplyByTimeScale);
}
public static void Shak