Unity實現(xiàn)彈球打磚塊游戲
本文實例為大家分享了Unity實現(xiàn)彈球打磚塊游戲的具體代碼,供大家參考,具體內容如下
創(chuàng)作界面記錄
攝像機
所需腳本
1射線shexian
using System.Collections; using System.Collections.Generic; using UnityEngine; public class sheixian : MonoBehaviour { ? ? public GameObject blit;//定義一個公共的游戲物體來當子彈 ? ? AudioSource au;//定義一個au音效 ? ? // Use this for initialization ? ? void Start () { ? ? ? ? au = GetComponent<AudioSource>();//賦值音效 ? ? } ? ? // Update is called once per frame ? ? void Update () { ? ? ? ? Ray ray; ? ? ? ? RaycastHit hit; ? ? ? ? //1.創(chuàng)建射線 ? ? ? ? //2.射線檢測并反饋結果 ? ? ? ? //鼠標左鍵點擊一個東西,然后反饋給我們物體信息 ? ? ? ? if (Input.GetMouseButtonDown(0)) ? ? ? ? { ? ? ? ? ? ? //把攝像機屏幕點轉化為線 ? ? ? ? ? ? 獲取鼠標坐標 ? ? ? ? ? ? ray = Camera.main.ScreenPointToRay(Input.mousePosition); ? ? ? ? ? ? //創(chuàng)建射線 ? ? ? ? ? ? if (Physics.Raycast(ray, out hit))//第一個參數(shù)是射線,第二個是碰撞的物體 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //定義一個bt承接實例化的子彈, 對象實例化(實例化對象,位置[攝像機當前位置],不旋轉) ? ? ? ? ? ? ? ? GameObject bt = GameObject.Instantiate(blit, transform.position, Quaternion.identity); ? ? ? ? ? ? ? ? au.Play();//播放音效 ? ? ? ? ? ? ? ? //給一個方向 ? 點擊位置的坐標-當前位置=一個向量; ? ? ? ? ? ? ? ? Vector3 dis = hit.point - transform.position; ? ? ? ? ? ? ? ? //給bt一個力 ? ? ? ? ? ? ? ? bt.GetComponent<Rigidbody>().AddForce(dis * 300); ? ? ? ? ? ? } ? ? ? ? } ? ? } }
把Sphere預制體拉入Blit框,添加Audio Source組件,AudioClip拉入子彈音效;取消Play On Awake
地板
給地板添加音樂來當背景音樂,再給個材質改變顏色
空物體copy
用來實例化磚塊,掛載copysp腳本
using System.Collections; using System.Collections.Generic; using UnityEngine; public class copysp : MonoBehaviour { ? ? public GameObject ga;//定義一個游戲物體 ? ? // Use this for initialization ? ? void Copy () { ? ?//實例化預制體 ? ? ? ? ? ? for (int i = 0; i < 25; i++)//用for循環(huán)來實現(xiàn)多個實例化 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //定義一個隨機向量 ? ? ? ? ?X坐標 ? ? ? ? ? ? ? ? ? ? ,Y坐標, ?Z坐標 ? ? ? ? ? ? ? ? Vector3 ve3 = new Vector3(Random.Range(-5.0f, 5.0f), 10.0f, Random.Range(-5.0f, 5.0f)); ? ? ? ? ? ? ? ? //實例化 ? ? 實例化物體,位置,是否旋轉(不旋轉) ? ? ? ? ? ? ? ? Instantiate(ga, ve3, Quaternion.identity); ? ? ? ? ? ? } ? ? ?? ? ? } ? ? void Start() ? ? { ? ? ? ? //延時多次調用 ? (調用的方法,延時幾秒,隔幾秒再次調用) ? ? ? ? InvokeRepeating("Copy", 2, 6); ? ? } ? ? // Update is called once per frame ? ? void Update () { ? ? } }
預制體Cube
給Box Collider一個反彈材質
***Unity物體碰撞時的反彈系數(shù):也即Physic Material的 Bounciness屬性。
一句話,給物體的Collider添加Material屬性即可
1、首先,物體要有Collider(BoxCollider, SphereCollider,PolygonCollider等)
2、創(chuàng)建一個Physic Material
Asset -> Create->Physic Material
看到Bounciness這個屬性,區(qū)間是0到1,可以小數(shù),其他暫不動。
0值:沒有彈力,1值:沒有能量損失的反彈。
3、賦值給Collider的Material屬性。
系統(tǒng)自帶這幾種物理材質
Bouncy:彈性材質。Ice:冰材質。Metal:金屬材質。Rubber:橡膠材質。Wood:木頭材質。*
如圖
給一個Random Color腳本
using UnityEngine; using System.Collections; public class RandomColor : MonoBehaviour { ? ? // Use this for initialization ? ? void Start() ? ? { ? ? ? ? //獲取組件 ? ? ?材質.顏色 ? ? ? ? this.gameObject.GetComponent<MeshRenderer>().material.color = RandomColor1();//給游戲物體添加下方隨機顏色方法 ? ? } ? ? /*float timer; ? ? //隨著時間變換顏色 ? ? // Update is called once per frame ? ? void Update() ? ? { ? ? ? ? timer -= Time.deltaTime; ? ? ? ? if (timer <= 0) ? ? ? ? { ? ? ? ? ? ? this.gameObject.GetComponent<MeshRenderer>().material.color = RandomColor1(); ? ? ? ? ? ? timer = 1; ? ? ? ? } ? ? }*/ ? ? public Color RandomColor1() ? ? { ? ? ? ? float r = Random.Range(0f, 1f); ? ? ? ? float g = Random.Range(0f, 1f); ? ? ? ? float b = Random.Range(0f, 1f); ? ? ? ? Color color = new Color(r, g, b); ? ? ? ? return color; ? ? } }
給一個Cube銷毀腳本
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Cubexiaohui : MonoBehaviour { ? ? // Use this for initialization ? ? void Start () { ? ? } ? ? // Update is called once per frame ? ? void Update () { ? ? ? ? if (this.transform.position.y < 0)//如果此物體的y坐標小于0,銷毀此物體; ? ? ? ? { ? ? ? ? ? ? Destroy(this.gameObject); ? ? ? ? } ? ? ? ? else ? ? ? ? { ? ? ? ? ? ? Destroy(this.gameObject,100);//或者100秒后銷毀 ? ? ? ? } ? ? } }
球體預制體Sphere
給個反彈材質,掛載隨機顏色腳本,掛載xiaohui腳本
using System.Collections; using System.Collections.Generic; using UnityEngine; public class xiaohui : MonoBehaviour { ? ? AudioSource audio;//銷毀的音效 ? ? // Use this for initialization ? ? void Start () { ? ? ? ? audio = GetComponent<AudioSource>();//承接一下音效組件 ? ? } ? ? void OnCollisionEnter(Collision coll)//被碰撞的形參 ? ? { ? ? ? ? if (coll.gameObject.tag == "Player")//如果碰到標簽為"Player"的物體,就銷毀它 ? ? ? ? { ? ? ? ? ? ? audio.Play();//播放音效 ? ? ? ? ? ? Destroy(coll.gameObject);//銷毀碰撞的物體 ? ? ? ? } ? ? } ? ? // Update is called once per frame ? ? void Update () { ? ? ? ? Destroy(this.gameObject, 10);//此物體十秒后自己消失 ? ? } }
添加銷毀音效,至此大功告成.
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
在C# WPF下自定義滾動條ScrollViewer樣式的操作
這篇文章主要介紹了在C# WPF下自定義滾動條ScrollViewer樣式的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01C# web應用程序不能訪問app_code下類的原因以及解決方法
本文主要介紹了C#web應用程序不能訪問app_code下類的原因以及解決方法。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02C#中String和StringBuilder的簡介與區(qū)別
今天小編就為大家分享一篇關于C#中String和StringBuilder的簡介與區(qū)別,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10Unity UGUI的InputField輸入框組件使用詳解
這篇文章主要為大家介紹了Unity UGUI的InputField輸入框組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07C# 使用鼠標點擊對Chart控件實現(xiàn)數(shù)據(jù)提示效果
這篇文章主要介紹了C# 使用鼠標點擊對Chart控件實現(xiàn)數(shù)據(jù)提示效果,文章給予上一篇的詳細內容做延伸介紹,需要的小伙伴可任意參考一下2022-08-08