Unity 屏幕震动效果实现
2014-05-06 17:27 wuzhang 阅读(7966) 评论(0) 编辑 收藏 举报要实现震屏效果其实并不难,所谓的震屏在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; } } } } }
红色标记即为调用震屏效果:
此时屏幕就会震动!