Unity實現(xiàn)打磚塊游戲
更新時間:2022年05月11日 14:41:09 作者:大聰明深夜a題
這篇文章主要為大家詳細(xì)介紹了Unity實現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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;//為子彈施加速度
? ? ? ? }
? ? }
}將這段代碼給攝像機就可以在運行游戲時按下鼠標(biāo)左鍵發(fā)射子彈
3.移動發(fā)射
將以下腳本代碼給攝像機就可以完成移動了
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#開發(fā)windows服務(wù)實現(xiàn)自動從FTP服務(wù)器下載文件
這篇文章主要為大家詳細(xì)介紹了C#開發(fā)windows服務(wù)實現(xiàn)自動從FTP服務(wù)器下載文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03

