Unity入門案例之打磚塊


  1. 案例目的:熟悉Unity各個界面的功能以及基本操作,以及基本規范
  2. 游戲方法:簡單的通過子彈擊落場景內的磚塊

 

 

  • 首先在項目根目錄中就要做好基礎的功能分類,文件夾分為  
    • Materials(用於存放貼圖、材質等)
    • Prefab(用於存放一些預制體,方便實例化等操作)
    • Scens(用於存放游戲場景)
    • Scripts(用於存放游戲腳本)
  • 素材准備:牆體(Cube堆出來的牆),子彈(Sphere),地板(Plane)
  • 腳本:Movemoent(用於控制攝像機移動),Shoot(用於控制子彈的射擊)

Movement.cs

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class Movemoent : MonoBehaviour
 6 {
 7     public float speed;
 8     void Update()
 9     {
10         float h = Input.GetAxis("Horizontal"); //檢測方向鍵左右,按左h會逐漸變成1,按右鍵會逐漸變成-1
11         float v = Input.GetAxis("Vertical");//檢測方向鍵上下,按上v會逐漸變成1,按下鍵會逐漸變成-1
12         transform.Translate(new Vector3(h, v, 0) * Time.deltaTime*speed);//Time.deltaTime是每幀分之一,相當於五十分之一
13     }
14 }

Shoot.cs

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class Shoot : MonoBehaviour
 6 {
 7     public GameObject bullet;
 8     public float speed;
 9     void Update()
10     {
11         if (Input.GetMouseButtonDown(0))
12         {
13             GameObject b = GameObject.Instantiate(bullet, transform.position, transform.rotation);
14             Rigidbody rgd = b.GetComponent<Rigidbody>();//獲取子彈的剛體組件
15             rgd.velocity = transform.forward * speed;//修改子彈剛體組件中速度的值
16         }
17     }
18 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM