Unity教程之-Unity3d实现屏幕抖动效果

 

很多游戏玩家游戏死亡时一般都需要屏幕抖一下下或者屏幕变灰。为了在unity3d中实现这个特效,百度了下相关写法,发现方法很多~~~,找来找去,找到个简单粗暴地,啥都不需要,Camera Viewpoint实现 一个脚本拖动到Camera上就可以了,略微修改了一点点,share一下,下面是代码


using UnityEngine;
using System.Collections;

public class ShakeCamera : MonoBehaviour
{

// 震动标志位
private bool isshakeCamera = false;

// 震动幅度
public float shakeLevel = 3f;
// 震动时间
public float setShakeTime = 0.2f;
// 震动的FPS
public float shakeFps = 45f;

private float fps;
private float shakeTime = 0.0f;
private float frameTime = 0.0f;
private float shakeDelta = 0.005f;
private Camera selfCamera;

void Awake()
{
selfCamera = GetComponent<Camera>();
}

// Use this for initialization
void Start()
{
shakeTime = setShakeTime;
fps = shakeFps;
frameTime = 0.03f;
shakeDelta = 0.005f;
}

// Update is called once per frame
void Update()
{
if (isshakeCamera)
{
if (shakeTime > 0)
{
shakeTime -= Time.deltaTime;
if (shakeTime <= 0)
{
selfCamera.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
isshakeCamera = false;
shakeTime = setShakeTime;
fps = shakeFps;
frameTime = 0.03f;
shakeDelta = 0.005f;
}
else
{
frameTime += Time.deltaTime;

if (frameTime > 1.0 / fps)
{
frameTime = 0;
selfCamera.rect = new Rect(shakeDelta * (-1.0f + shakeLevel * Random.value), shakeDelta * (-1.0f + shakeLevel * Random.value), 1.0f, 1.0f);
}
}
}
}
}

public void shake()
{
isshakeCamera = true;
}
}

 

好了本篇unity3d教程到此结束,下篇我们再会!