- 案例目的:熟悉Unity各个界面的功能以及基本操作,以及基本规范
- 游戏方法:简单的通过子弹击落场景内的砖块
- 首先在项目根目录中就要做好基础的功能分类,文件夹分为
- 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 }