欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity實現(xiàn)彈球打磚塊游戲

 更新時間:2022年05月11日 14:28:16   作者:璐希法  
這篇文章主要為大家詳細(xì)介紹了Unity實現(xiàn)彈球打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Unity實現(xiàn)彈球打磚塊游戲的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)作界面記錄

攝像機

所需腳本

1射線shexian

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class sheixian : MonoBehaviour {
? ? public GameObject blit;//定義一個公共的游戲物體來當(dāng)子彈
? ? 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.射線檢測并反饋結(jié)果
? ? ? ? //鼠標(biāo)左鍵點擊一個東西,然后反饋給我們物體信息
? ? ? ? if (Input.GetMouseButtonDown(0))
? ? ? ? {
? ? ? ? ? ? //把攝像機屏幕點轉(zhuǎn)化為線 ? ? ? ? ? ? 獲取鼠標(biāo)坐標(biāo)
? ? ? ? ? ? ray = Camera.main.ScreenPointToRay(Input.mousePosition);
? ? ? ? ? ? //創(chuàng)建射線
? ? ? ? ? ? if (Physics.Raycast(ray, out hit))//第一個參數(shù)是射線,第二個是碰撞的物體
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //定義一個bt承接實例化的子彈, 對象實例化(實例化對象,位置[攝像機當(dāng)前位置],不旋轉(zhuǎn))
? ? ? ? ? ? ? ? GameObject bt = GameObject.Instantiate(blit, transform.position, Quaternion.identity);
? ? ? ? ? ? ? ? au.Play();//播放音效
? ? ? ? ? ? ? ? //給一個方向 ? 點擊位置的坐標(biāo)-當(dāng)前位置=一個向量;
? ? ? ? ? ? ? ? Vector3 dis = hit.point - transform.position;
? ? ? ? ? ? ? ? //給bt一個力
? ? ? ? ? ? ? ? bt.GetComponent<Rigidbody>().AddForce(dis * 300);
? ? ? ? ? ? }
? ? ? ? }
? ? }

}

把Sphere預(yù)制體拉入Blit框,添加Audio Source組件,AudioClip拉入子彈音效;取消Play On Awake

地板

給地板添加音樂來當(dāng)背景音樂,再給個材質(zhì)改變顏色

空物體copy

用來實例化磚塊,掛載copysp腳本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class copysp : MonoBehaviour {
? ? public GameObject ga;//定義一個游戲物體


? ? // Use this for initialization
? ? void Copy () { ? ?//實例化預(yù)制體

? ? ? ? ? ? for (int i = 0; i < 25; i++)//用for循環(huán)來實現(xiàn)多個實例化
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //定義一個隨機向量 ? ? ? ? ?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));
? ? ? ? ? ? ? ? //實例化 ? ? 實例化物體,位置,是否旋轉(zhuǎn)(不旋轉(zhuǎn))
? ? ? ? ? ? ? ? Instantiate(ga, ve3, Quaternion.identity);
? ? ? ? ? ? } ? ? ??
? ? }

? ? void Start()
? ? {
? ? ? ? //延時多次調(diào)用 ? (調(diào)用的方法,延時幾秒,隔幾秒再次調(diào)用)
? ? ? ? InvokeRepeating("Copy", 2, 6);
? ? }
? ? // Update is called once per frame
? ? void Update () {

? ? }
}

預(yù)制體Cube

給Box Collider一個反彈材質(zhì)
***Unity物體碰撞時的反彈系數(shù):也即Physic Material的 Bounciness屬性?! ?/p>

一句話,給物體的Collider添加Material屬性即可

1、首先,物體要有Collider(BoxCollider, SphereCollider,PolygonCollider等)

2、創(chuàng)建一個Physic Material

Asset -> Create->Physic Material

看到Bounciness這個屬性,區(qū)間是0到1,可以小數(shù),其他暫不動。
0值:沒有彈力,1值:沒有能量損失的反彈?! ?/p>

3、賦值給Collider的Material屬性。

系統(tǒng)自帶這幾種物理材質(zhì)

Bouncy:彈性材質(zhì)。Ice:冰材質(zhì)。Metal:金屬材質(zhì)。Rubber:橡膠材質(zhì)。Wood:木頭材質(zhì)。*

如圖

給一個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();//給游戲物體添加下方隨機顏色方法
? ? }
? ? /*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坐標(biāo)小于0,銷毀此物體;
? ? ? ? {
? ? ? ? ? ? Destroy(this.gameObject);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? Destroy(this.gameObject,100);//或者100秒后銷毀
? ? ? ? }
? ? }
}

球體預(yù)制體Sphere

給個反彈材質(zhì),掛載隨機顏色腳本,掛載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")//如果碰到標(biāo)簽為"Player"的物體,就銷毀它
? ? ? ? {
? ? ? ? ? ? audio.Play();//播放音效
? ? ? ? ? ? Destroy(coll.gameObject);//銷毀碰撞的物體
? ? ? ? }
? ? }
? ? // Update is called once per frame
? ? void Update () {
? ? ? ? Destroy(this.gameObject, 10);//此物體十秒后自己消失
? ? }
}

添加銷毀音效,至此大功告成.

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論