Unity——滚动的小球
Unity——滚动的小球
工程理解
本游戏为通过键盘上的W、A、S、D键控制小球的运动轨迹来对固定位置上的小方块进行碰撞,以此来进行加分计数的。
方块进行碰撞检测。
效果展示
核心代码
PlayerController:
一、移动功能
用unity自带的
1 Input.GetAxis("")
函数,Horizontal代表键盘的左右键(A和D键);Vertical代表键盘的上下键(W和S键)。然后用AddForce给予小球一个外力使其按照
相应的方向动起来。
1 //通过键盘控制小球移动 2 private void FixedUpdate() 3 { 4 var moveHorizontal = Input.GetAxis("Horizontal"); //水平方向 5 var moveVertical = Input.GetAxis("Vertical");//垂直方向 6 var movement = new Vector3(moveHorizontal, 0, moveVertical); 7 //Vector3三维向量 8 m_Rigidbody.AddForce(movement * Speed); 9 //Speed 为速度 通过movement和Speed来控制小球的方向和速度 10 }
二、计数功能
-
定义了一个m_Count变量来计数小球碰到方块的个数;
-
并通过
1 GameObject.Destroy(other.gameObject);
语句,来对已经计数的方块进行销毁处理;(我们给方块修改标签”Tag”,如果小球碰撞的物体标签是方块的标签名,我们则销毁此方块并让分数加一)
-
调用
1 SetCountText();
函数,将计数传给UI进行显示。
1 //计数功能 2 private void OnTriggerEnter(Collider other) 3 { 4 if (other.gameObject.CompareTag("Pick Up")) 5 { 6 GameObject.Destroy(other.gameObject);//销毁已记数方块 7 m_Count++;//计数 8 SetCountText();//调用函数,将个数传给UI进行显示 9 } 10 }
三、UI显示功能
-
将计数功能所记的数目传递过来,通过
1 CountText.text = m_Count.ToString();//计数UI显示
语句,将所计数目通过UI显示,其中CountText.text为UI计数的变量。
-
当所有小方块都被碰撞到时,即m_Count >= 8(我这里给的小方块个数为8个),输出游戏成功信息,并储存在WinText.text中。
1 private void SetCountText() 2 { 3 CountText.text = m_Count.ToString();//计数UI显示 4 5 if (m_Count >= 8) 6 { 7 WinText.text = "You Win!";//成功提示 8 } 9 }
CameraController和Rotator:
1 public class CameraController : MonoBehaviour 2 { 3 public GameObject Player; 4 private Vector3 m_Offset; 5 // Start is called before the first frame update 6 void Start() 7 { 8 m_Offset = transform.position - Player.transform.position; 9 } 10 11 // Update is called once per frame 12 void Update() 13 { 14 transform.position = Player.transform.position + m_Offset; 15 } 16 }
1 public class Rotator : MonoBehaviour 2 { 3 4 // Update is called once per frame 5 void Update() 6 { 7 transform.Rotate(new Vector3(15,30,45) * Time.deltaTime); 8 } 9 }
改进——框架
由于,假设将小方块个数改为10个,则:
-
显示得分的UI是写在一起的,当修改相对应的限制条件时,会受到牵连
-
判定输赢的逻辑也会受到牵连
-
显示输赢结算UI逻辑也在一起,也会受到牵连
所以利用框架进行改进
在框架中,
-
Player:负责接收玩家输入并响应角色移动;当碰撞到小方块时,通知Game Mode
-
Game Mode:对全局的游戏规则、玩法、计分等内容进行处理;在Player控制器通知Game Mode碰撞到小方块后,Game Mode进行对应的计分和输赢判断等操作
-
小方块:在游戏启动时把自己的存在告知Game Mode;这样一来,Game Mode就完全直到当前场上有多少个小方块,就可以动态的根据场上方块数量来判断输赢,而不会在代码中写死逻辑了。
相关代码:
PickupComponent:
1 public class PickupComponent : MonoBehaviour 2 { 3 // Start is called before the first frame update 4 private void Start() 5 { 6 //把自己注册到GameMode 7 GameMode.Instance.RegisterPickUp(this); 8 } 9 }
PlayerController控制器:
1 public class PlayerController : MonoBehaviour 2 { 3 public float Speed; 4 5 private Rigidbody m_Rigidbody; 6 7 private void Start() 8 { 9 m_Rigidbody = this.GetComponent<Rigidbody>(); 10 } 11 12 private void FixedUpdate() 13 { 14 var moveHorizontal = Input.GetAxis("Horizontal"); 15 var moveVertical = Input.GetAxis("Vertical"); 16 17 var movement = new Vector3(moveHorizontal, 0, moveVertical); 18 19 m_Rigidbody.AddForce(movement * Speed); 20 } 21 22 private void OnTriggerEnter(Collider other) 23 { 24 if (other.gameObject.CompareTag("Pick Up")) 25 { 26 var pickupComponent = other.gameObject.GetComponent<PickupComponent>(); 27 if (pickupComponent == null) 28 { 29 Debug.LogError("有一个Tag是Pickup的GameObject,却没有挂载Pickup相关组件"); 30 } 31 GameMode.Instance.PlayerEnterPickup(pickupComponent); 32 } 33 } 34 35 36 }
GameMode,统一管理游戏的玩法、得分判定、输赢结果等内容的类:
1 public class GameMode : MonoBehaviour 2 { 3 public static GameMode Instance { get; private set; } 4 [Header("计分Text")] 5 public Text CountText; 6 7 [Header("显示Win的文本")] 8 public Text WinText; 9 10 private readonly List<PickupComponent> m_Pickups = new List<PickupComponent>(); 11 12 private int _count; 13 private int m_Count 14 { 15 get => _count; 16 set 17 { 18 _count = value; 19 if (CountText != null) 20 { 21 CountText.text = value.ToString(); 22 } 23 } 24 } 25 26 private void Awake() 27 { 28 if (Instance == null) 29 Instance = this; 30 } 31 public void RegisterPickUp(PickupComponent pickup) 32 { 33 m_Pickups.Add(pickup); 34 } 35 public void PlayerEnterPickup(PickupComponent pickup) 36 { 37 m_Pickups.Remove(pickup); 38 GameObject.Destroy(pickup.gameObject); 39 m_Count++; 40 if (m_Pickups.Count <= 0) 41 { 42 WinText.text = "You Win!"; 43 } 44 } 45 46 private void Start() 47 { 48 WinText.text = string.Empty; 49 m_Count = 0; 50 } 51 52 private void Update() 53 { 54 if (m_Pickups.Count > 0) 55 { 56 var rotateValue = new Vector3(15, 30, 45) * Time.deltaTime; 57 foreach (var pickup in m_Pickups) 58 { 59 pickup.transform.Rotate(rotateValue); 60 } 61 } 62 } 63 } 64