本游戏为通过键盘上的W、A、S、D键控制小球的运动轨迹来对固定位置上的小方块进行碰撞,以此来进行加分计数的。
其中主要对象为小球和自转的小方块;在小球上,我们添加刚体和碰撞盒组件。刚体用于模拟真实的物理运动,碰撞盒子则用于与后面的
方块进行碰撞检测。
PlayerController:
用unity自带的
登录后复制
1 Input.GetAxis("")1.
函数,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 }1.
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 }1.
登录后复制
1.
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 }1.
登录后复制
1.
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 frame12 void Update()13 {14 transform.position = Player.transform.position + m_Offset;15 }16 }1.
登录后复制
1 public class Rotator : MonoBehaviour2 {3 4 // Update is called once per frame5 void Update()6 {7 transform.Rotate(new Vector3(15,30,45) * Time.deltaTime);8 }9 }1.
由于,假设将小方块个数改为10个,则:
所以利用框架进行改进
在框架中,
相关代码:
PickupComponent:
登录后复制
1 public class PickupComponent : MonoBehaviour2 {3 // Start is called before the first frame update4 private void Start()5 {6 //把自己注册到GameMode7 GameMode.Instance.RegisterPickUp(this);8 }9 }1.
登录后复制
1.
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();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();
27 if (pickupComponent == null)28 {29 Debug.LogError("有一个Tag是Pickup的GameObject,却没有挂载Pickup相关组件");30 }31 GameMode.Instance.PlayerEnterPickup(pickupComponent);32 }33 }34 35 36 }1.2.
登录后复制
1.
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 Listm_Pickups = new List();11 12 private int _count;13 private int m_Count14 {15 get => _count;16 set17 {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 }641.2.
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删