Unity实现震屏效果

震屏效果,网上有很多,不过还是自己写一遍吧......虽然也是从网上借鉴来的......

代码如下:

using UnityEngine;
using System.Collections;

public class ShakeCamera : MonoBehaviour
{
    //是否开始抖动
    public bool isShake = false;
    //抖动幅度
    public float shakeLv = 3;
    //设置抖屏的时长
    public float setShakeTime = 0.2f;
    public float shakeFps = 45;
    float fps;
    float shakeTime = 0;
    float frameTime = 0f;
    float shakeDelta = 0.005f;
    //相机
    Camera selfCamers;
    //抖屏幅度
    Rect changeRect;
    void Start()
    {
        selfCamers = GetComponent();
        changeRect = new Rect(0, 0, 1, 1);
        shakeTime = setShakeTime;
        fps = shakeFps;
        frameTime = 0.03f;
        shakeDelta = 0.005f;
    }
    void Update()
    {
        if (isShake)
        {
            if (shakeTime > 0)
            {
                shakeTime -= Time.deltaTime;
                if (shakeTime <= 0)
                {
                    changeRect.xMin = 0;
                    changeRect.yMin = 0;
                    selfCamers.rect = changeRect;
                    isShake = false;
                    shakeTime = setShakeTime;
                    fps = shakeFps;
                    frameTime = 0.03f;
                    shakeDelta = 0.005f;
                }
                else
                {
                    frameTime += Time.deltaTime;
                    if (frameTime > 1.0 / shakeFps)
                    {
                        frameTime = 0;
                        changeRect.xMin = shakeDelta * (-1 + shakeLv * Random.value);
                        changeRect.yMin = shakeDelta * (-1 + shakeLv * Random.value);
                        selfCamers.rect = changeRect;
                    }
                }
            }
        }
    }
}

将以上代码挂载在相机上,调用如下:

GameObject.Find("Main Camera").GetComponent().isShake = true;

只用设置isShake的value就可以了。

你可能感兴趣的:(unity)