Unity實現(xiàn)打磚塊游戲
本文實例為大家分享了Unity實現(xiàn)打磚塊游戲的具體代碼,供大家參考,具體內(nèi)容如下
效果演示
1.創(chuàng)建墻
1.1我們用預(yù)制體來統(tǒng)一管理墻
方便以后對墻進(jìn)行修改
1.2我們還需要給磚塊一個剛體組件(物理屬性),不然墻就固定在那里不動。
1.3 把磚塊弄出來 再弄成一堵墻
2.發(fā)射子彈
我們將子彈也用預(yù)制體的方式創(chuàng)造。
這時就到了我們寫代碼的時候了。
using System.Collections; using System.Collections.Generic; using UnityEngine; ? public class shoot : MonoBehaviour { ? ? public GameObject bullet;//定義游戲物體 (子彈) ? ? float speed = 30;//子彈速度 ? ? // Start is called before the first frame update ? ? void Start() ? ? { ? ? ? ? ? ? } ? ? ? // Update is called once per frame ? ? void Update() ? ? { ? ? ? ? if(Input.GetMouseButtonDown(0))//是否按下鼠標(biāo)左鍵 ? ? ? ? { ? ? ? ? ? ?? ? ? ? ? ? ?GameObject b=GameObject.Instantiate(bullet,transform.position,transform.rotation);//生成子彈的位置 ? ? ? ? ? ? Rigidbody rd = b.GetComponent<Rigidbody>();//得到生成子彈的剛體組件 ? ? ? ? ? ? rd.velocity = transform.forward * speed;//為子彈施加速度 ? ? ? ? } ? ? } }
將這段代碼給攝像機(jī)就可以在運行游戲時按下鼠標(biāo)左鍵發(fā)射子彈
3.移動發(fā)射
將以下腳本代碼給攝像機(jī)就可以完成移動了
using System.Collections; using System.Collections.Generic; using UnityEngine; ? public class hv: MonoBehaviour { ? ? // Start is called before the first frame update ? ? void Start() ? ? { ? ? ? ?? ? ? } ? ? ? // Update is called once per frame ? ? void Update() ? ? { ? ? ? ? float h=Input.GetAxis("Horizontal");//水平方向 ? ? ? ? float v = Input.GetAxis("Vertical");//垂直方向 ? ? ? ? transform.Translate(new Vector3(h, v, 0)/60);//移動速度 ? ? } }
4.檢驗
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c# winform窗口一直置頂顯示在桌面最上方或最底層的方法
winform窗口一直置頂顯示在桌面最上方,這樣的功能真的很實用的,很多的軟件窗口都有這樣的功能,本文也來實現(xiàn)一個,感興趣的你千萬不要錯過了,希望本文對你有所幫助2013-01-01Unity3D Shader實現(xiàn)動態(tài)屏幕遮罩
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實現(xiàn)動態(tài)屏幕遮罩效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02WinForm DataGridView控件隔行變色的小例子
WinForm的DataGridView控件設(shè)置行的顏色2013-03-03C#定制Excel界面并實現(xiàn)與數(shù)據(jù)庫交互的方法
這篇文章主要介紹了C#定制Excel界面并實現(xiàn)與數(shù)據(jù)庫交互的方法的相關(guān)資料,需要的朋友可以參考下2015-11-11C#實現(xiàn)控制電腦注銷,關(guān)機(jī)和重啟
這篇文章主要為大家介紹了C#如何實現(xiàn)控制電腦注銷,關(guān)機(jī)和重啟功能,本案例涉及的知識點包含:Process、Shell32.dll、User32.dll、Struct數(shù)據(jù)結(jié)構(gòu),感興趣的可以了解一下2022-09-09