Unity實(shí)現(xiàn)彈球打磚塊游戲
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)彈球打磚塊游戲的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)作界面記錄
攝像機(jī)

所需腳本
1射線(xiàn)shexian
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sheixian : MonoBehaviour {
? ? public GameObject blit;//定義一個(gè)公共的游戲物體來(lái)當(dāng)子彈
? ? AudioSource au;//定義一個(gè)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)建射線(xiàn)
? ? ? ? //2.射線(xiàn)檢測(cè)并反饋結(jié)果
? ? ? ? //鼠標(biāo)左鍵點(diǎn)擊一個(gè)東西,然后反饋給我們物體信息
? ? ? ? if (Input.GetMouseButtonDown(0))
? ? ? ? {
? ? ? ? ? ? //把攝像機(jī)屏幕點(diǎn)轉(zhuǎn)化為線(xiàn) ? ? ? ? ? ? 獲取鼠標(biāo)坐標(biāo)
? ? ? ? ? ? ray = Camera.main.ScreenPointToRay(Input.mousePosition);
? ? ? ? ? ? //創(chuàng)建射線(xiàn)
? ? ? ? ? ? if (Physics.Raycast(ray, out hit))//第一個(gè)參數(shù)是射線(xiàn),第二個(gè)是碰撞的物體
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //定義一個(gè)bt承接實(shí)例化的子彈, 對(duì)象實(shí)例化(實(shí)例化對(duì)象,位置[攝像機(jī)當(dāng)前位置],不旋轉(zhuǎn))
? ? ? ? ? ? ? ? GameObject bt = GameObject.Instantiate(blit, transform.position, Quaternion.identity);
? ? ? ? ? ? ? ? au.Play();//播放音效
? ? ? ? ? ? ? ? //給一個(gè)方向 ? 點(diǎn)擊位置的坐標(biāo)-當(dāng)前位置=一個(gè)向量;
? ? ? ? ? ? ? ? Vector3 dis = hit.point - transform.position;
? ? ? ? ? ? ? ? //給bt一個(gè)力
? ? ? ? ? ? ? ? bt.GetComponent<Rigidbody>().AddForce(dis * 300);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}把Sphere預(yù)制體拉入Blit框,添加Audio Source組件,AudioClip拉入子彈音效;取消Play On Awake
地板
給地板添加音樂(lè)來(lái)當(dāng)背景音樂(lè),再給個(gè)材質(zhì)改變顏色

空物體copy
用來(lái)實(shí)例化磚塊,掛載copysp腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class copysp : MonoBehaviour {
? ? public GameObject ga;//定義一個(gè)游戲物體
? ? // Use this for initialization
? ? void Copy () { ? ?//實(shí)例化預(yù)制體
? ? ? ? ? ? for (int i = 0; i < 25; i++)//用for循環(huán)來(lái)實(shí)現(xiàn)多個(gè)實(shí)例化
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //定義一個(gè)隨機(jī)向量 ? ? ? ? ?X坐標(biāo) ? ? ? ? ? ? ? ? ? ? ,Y坐標(biāo), ?Z坐標(biāo)
? ? ? ? ? ? ? ? Vector3 ve3 = new Vector3(Random.Range(-5.0f, 5.0f), 10.0f, Random.Range(-5.0f, 5.0f));
? ? ? ? ? ? ? ? //實(shí)例化 ? ? 實(shí)例化物體,位置,是否旋轉(zhuǎn)(不旋轉(zhuǎn))
? ? ? ? ? ? ? ? Instantiate(ga, ve3, Quaternion.identity);
? ? ? ? ? ? } ? ? ??
? ? }
? ? void Start()
? ? {
? ? ? ? //延時(shí)多次調(diào)用 ? (調(diào)用的方法,延時(shí)幾秒,隔幾秒再次調(diào)用)
? ? ? ? InvokeRepeating("Copy", 2, 6);
? ? }
? ? // Update is called once per frame
? ? void Update () {
? ? }
}預(yù)制體Cube

給Box Collider一個(gè)反彈材質(zhì)
***Unity物體碰撞時(shí)的反彈系數(shù):也即Physic Material的 Bounciness屬性?! ?/p>
一句話(huà),給物體的Collider添加Material屬性即可
1、首先,物體要有Collider(BoxCollider, SphereCollider,PolygonCollider等)
2、創(chuàng)建一個(gè)Physic Material
Asset -> Create->Physic Material
看到Bounciness這個(gè)屬性,區(qū)間是0到1,可以小數(shù),其他暫不動(dòng)。
0值:沒(méi)有彈力,1值:沒(méi)有能量損失的反彈?! ?/p>
3、賦值給Collider的Material屬性。
系統(tǒng)自帶這幾種物理材質(zhì)
Bouncy:彈性材質(zhì)。Ice:冰材質(zhì)。Metal:金屬材質(zhì)。Rubber:橡膠材質(zhì)。Wood:木頭材質(zhì)。*
如圖

給一個(gè)Random Color腳本
using UnityEngine;
using System.Collections;
public class RandomColor : MonoBehaviour
{
? ? // Use this for initialization
? ? void Start()
? ? {
? ? ? ? //獲取組件 ? ? ?材質(zhì).顏色
? ? ? ? this.gameObject.GetComponent<MeshRenderer>().material.color = RandomColor1();//給游戲物體添加下方隨機(jī)顏色方法
? ? }
? ? /*float timer;
? ? //隨著時(shí)間變換顏色
? ? // 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;
? ? }
}給一個(gè)Cube銷(xiāo)毀腳本
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坐標(biāo)小于0,銷(xiāo)毀此物體;
? ? ? ? {
? ? ? ? ? ? Destroy(this.gameObject);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? Destroy(this.gameObject,100);//或者100秒后銷(xiāo)毀
? ? ? ? }
? ? }
}球體預(yù)制體Sphere

給個(gè)反彈材質(zhì),掛載隨機(jī)顏色腳本,掛載xiaohui腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class xiaohui : MonoBehaviour {
? ? AudioSource audio;//銷(xiāo)毀的音效
? ? // Use this for initialization
? ? void Start () {
? ? ? ? audio = GetComponent<AudioSource>();//承接一下音效組件
? ? }
? ? void OnCollisionEnter(Collision coll)//被碰撞的形參
? ? {
? ? ? ? if (coll.gameObject.tag == "Player")//如果碰到標(biāo)簽為"Player"的物體,就銷(xiāo)毀它
? ? ? ? {
? ? ? ? ? ? audio.Play();//播放音效
? ? ? ? ? ? Destroy(coll.gameObject);//銷(xiāo)毀碰撞的物體
? ? ? ? }
? ? }
? ? // Update is called once per frame
? ? void Update () {
? ? ? ? Destroy(this.gameObject, 10);//此物體十秒后自己消失
? ? }
}添加銷(xiāo)毀音效,至此大功告成.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#區(qū)分中英文按照指定長(zhǎng)度截取字符串的方法
這篇文章主要介紹了C#區(qū)分中英文按照指定長(zhǎng)度截取字符串的方法,涉及C#操作字符串的正則匹配與截取等常用操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
C#實(shí)現(xiàn)String字符串轉(zhuǎn)化為SQL語(yǔ)句中的In后接的參數(shù)詳解
在本篇文章中小編給大家分享的是一篇關(guān)于C#實(shí)現(xiàn)String字符串轉(zhuǎn)化為SQL語(yǔ)句中的In后接的實(shí)例內(nèi)容和代碼,需要的朋友們參考下。2020-01-01
C#串口編程System.IO.Ports.SerialPort類(lèi)
這篇文章介紹了C#串口編程System.IO.Ports.SerialPort類(lèi),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
C#中實(shí)現(xiàn)向數(shù)組中動(dòng)態(tài)添加元素
這篇文章主要介紹了C#中實(shí)現(xiàn)向數(shù)組中動(dòng)態(tài)添加元素方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
C# Console利用mspaint打開(kāi)圖像并保存的方法
這篇文章主要介紹了C# Console利用mspaint打開(kāi)圖像并保存的方法,涉及C#調(diào)用畫(huà)圖板操作圖片的相關(guān)技巧,需要的朋友可以參考下2016-01-01

