Unity功能 屏幕震动、抖动效果

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// boss攻击玩家产生的震动方法
/// 挂载到主摄像机上
/// 
public class Shake : MonoBehaviour {


    private float cameraShake = 2;//震动系数
    public GameObject UI;//红色的背景图片

	
    void Update () {
        if (Gun.Instance.bossAttack)
        {

            UI.SetActive(true);
            //X,Y轴震动
            transform.position = new Vector3((Random.Range(0f, cameraShake)) - cameraShake*0.5f, transform.position.y, transform.position.z);
            //Z轴震动
            transform.position = new Vector3(transform.position.x, transform.position.y, (Random.Range(0f, cameraShake)) - cameraShake * 0.5f);
            cameraShake = cameraShake / 1.05f;
            if (cameraShake<0.05f)
            {

                cameraShake= 0;
                UI.SetActive(false);
                Gun.Instance.bossAttack = false;
            }
        }
        else
        {
            cameraShake = 5;
        }
    }
}

 

你可能感兴趣的:(Unity功能)