Unity 屏幕震动效果实现

要实现震屏效果其实并不难,所谓的震屏在PC端可以简单地理解为相机(MainCamera)的抖动。

代码实现如下:

using UnityEngine;
using System.Collections;
public class ScreenShake : MonoBehaviour 
{
    private float shakeTime = 0.0f;
    private float fps= 20.0f;
    private float frameTime =0.0f;
    private float shakeDelta =0.005f;
    public  Camera cam ;
    public static bool isshakeCamera =false;
    // Use this for initialization
    void Start ()
    {
        shakeTime = 2.0f;
        fps= 20.0f;
        frameTime =0.03f;
        shakeDelta =0.005f;
    //isshakeCamera=true; }
// Update is called once per frame void Update () { if (isshakeCamera) { if(shakeTime > 0) { shakeTime -= Time.deltaTime; if(shakeTime <= 0) { cam.rect = new Rect(0.0f,0.0f,1.0f,1.0f); isshakeCamera =false; shakeTime = 1.0f; fps= 20.0f; frameTime =0.03f; shakeDelta =0.005f; } else { frameTime += Time.deltaTime; if(frameTime > 1.0 / fps) { frameTime = 0; cam.rect = new Rect(shakeDelta * ( -1.0f + 2.0f * Random.value),shakeDelta * ( -1.0f + 2.0f * Random.value), 1.0f, 1.0f); } } } } } }

实现代码就完成了,如何使用呢?

观察发现其实要想使用震屏效果只需isshakeCamera=true即可。

简单测试:把Start()函数取消注释即可。

因此我可以在我的游戏中的爆炸效果调用启用震屏脚本。

using UnityEngine;
using System.Collections;

public class planFly2 : MonoBehaviour 
{
    public static int  speed = 40;
    public GameObject airplne; 

    public GameObject stone;
    public GameObject  gobj_boom;

    public static float planeX;

    public AudioSource music;
    public float musicVolume;

    public static bool autoflystart=false; //自动飞行信号
    public static bool actionstart=false; //开始行动

    Vector3 vectauto=Vector3.left;

    // Use this for initialization
    void Start () 
    {    
        musicVolume=1f;
    }

    IEnumerator autoFly()
    {
        while(actionstart)
        {
            airplne.transform.Translate(vectauto* Time.deltaTime * speed,Space.World);
            planeX=airplne.transform.position.x;
            if(airplne.transform.position.x<=-55||airplne.transform.position.x>=52)
            {
                vectauto=-vectauto;
                if(airplne.transform.position.x==0)
                {
                    yield return new WaitForSeconds(1f);
                }
            }
            yield return new WaitForSeconds(0.02f);
        }
    }
    
    // Update is called once per frame
    void Update () 
    {
        if(((Input.GetKey(KeyCode.W)||(Input.GetKey(KeyCode.UpArrow)))&&airplne.transform.position.y<95))
        {
            actionstart=false;//解除自动飞行
            autoflystart=false;
            airplne.transform.Translate(Vector3.up* Time.deltaTime * speed,Space.World);
        }
        if((Input.GetKey(KeyCode.D)||(Input.GetKey(KeyCode.RightArrow)))&&airplne.transform.position.x<50)
        {
            actionstart=false;
            autoflystart=false;
            airplne.transform.Translate(Vector3.right* Time.deltaTime * speed,Space.World);
            planeX=airplne.transform.position.x;
        }
        if((Input.GetKey(KeyCode.A)||(Input.GetKey(KeyCode.LeftArrow)))&&airplne.transform.position.x>-50)
        {
            actionstart=false;
            autoflystart=false;
            airplne.transform.Translate(Vector3.left* Time.deltaTime * speed,Space.World);
            planeX=airplne.transform.position.x;
        }
        if((Input.GetKey(KeyCode.S)||(Input.GetKey(KeyCode.DownArrow)))&&airplne.transform.position.y>47)
        {
            actionstart=false;
            autoflystart=false;
            airplne.transform.Translate(Vector3.down* Time.deltaTime * speed,Space.World);
        }
        if(Input.GetKeyDown(KeyCode.Q))
        {
            StopAllCoroutines();
            autoflystart=!autoflystart;
            actionstart=true;
            if(autoflystart)
            {
                StartCoroutine(autoFly());
            }
        }
        if(ui.signal)
        {
            airplne.transform.Translate(Vector3.up*1,Space.World);
        }
        if(ui.stopAll)
        {
            transform.gameObject.SetActive(false);
        }
    }

    void OnCollisionEnter2D(Collision2D coll) //石头砸到飞机 飞机减血
    {
        if(coll.transform.tag!="plane2_bullet")
        {
            if(coll.gameObject.tag=="coin")
            {
                ui.gold++;
                music.Play();
                Destroy(coll.gameObject);
                ScreenShake.isshakeCamera=true;
            }
            else
            {
                if(coll.gameObject.tag!="jiguang")
                {
                    Destroy(coll.transform.gameObject);
                    GameObject tm_boom = Instantiate(gobj_boom,coll.transform.position,new Quaternion()) as GameObject;
                    Destroy(tm_boom,2);//实例后销毁粒子效果
                    ui.bloods-=5;
                    ScreenShake.isshakeCamera=true;
                }
            }
        }
    }
}

红色标记即为调用震屏效果:

此时屏幕就会震动!

转载于:https://www.cnblogs.com/wuzhang/p/wuzhang20140506.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值