要實現震屏效果其實並不難,所謂的震屏在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; } } } } }
紅色標記即為調用震屏效果:
此時屏幕就會震動!