第一個小遊戲的開發過程,小球跑酷

記錄一下今天,本身的第一個小遊戲完成了,心情很舒爽。很是很是簡單的小遊戲,下面梳理一下過程。canvas

首先建立跑道,使用cube,拉長等等。而後建立小球sphere,小球即爲玩家(player,積累單詞)。再添加障礙物,在分別爲其上色。ide

緊接着給小球添加腳本,讓其可以受玩家控制測試

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using UnityEngine.SceneManagement;
 5 
 6 public class Player : MonoBehaviour
 7 {
 8     //初始速度
 9     public float speed = 10;
10     //初始轉彎速度
11     public float turnSpeed = 4;
12 
13     // Start is called before the first frame update
14     void Start()
15     {
16 
17     }
18 
19     // Update is called once per frame
20     void Update()
21     {
22         //按R鍵重玩,場景重置
23         if (Input.GetKeyDown(KeyCode.R)){
24             SceneManager.LoadScene(0);
25             Time.timeScale = 1;
26             //結束當前方法,防止一直掉下去
27             return;
28         }
29 
30         //獲取座標
31         float x = Input.GetAxis("Horizontal");
32         //給物體一個變化,即x軸會有一個轉彎速度,y軸不變,z軸也有一個速度
33         transform.Translate(x * turnSpeed * Time.deltaTime, 0, speed * Time.deltaTime);
34 
35         //判斷位置,若是在範圍以外,則讓它有其餘的運轉動做
36         if (transform.position.x < -4 || transform.position.x > 4)
37             transform.Translate(0, -10 * Time.deltaTime, 0);
38 
39         //若是y軸小於-20,則中止
40         if (transform.position.y < -20)
41             Time.timeScale = 0;
42 
43 
44     }
45 }

給小球添加剛體組件rigidbody,使其與障礙物barrier發生碰撞,也要給障礙物添加觸發器trigger,建立barrier腳本spa

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class Barrier : MonoBehaviour
 6 {
 7     // Start is called before the first frame update
 8     void Start()
 9     {
10         
11     }
12 
13     private void OnTriggerEnter(Collider other)
14     {
15         //debug測試是否發生碰撞
16         Debug.Log(other.name + "碰到了我");
17         //若是和本身碰撞的物體名稱是player就中止
18         if(other.name == "Player")
19             Time.timeScale = 0;
20 
21     }
22 }

此時功能已經基本完成,在建立遊戲通關完成UI,遊戲開始時隱藏UI,觸發是顯示UI,建立一個物體將該腳本掛在上面,此物體放在小球跑道上。此物體也是觸發器。debug

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class Finish : MonoBehaviour
 6 {
 7     // Start is called before the first frame update
 8     void Start()
 9     {
10         GameObject canvas = GameObject.Find("Canvas");
11         canvas.transform.Find("Panel").gameObject.SetActive(false);
12     }
13 
14     private void OnTriggerEnter(Collider other)
15     {
16         GameObject canvas = GameObject.Find("Canvas");
17         canvas.transform.Find("Panel").gameObject.SetActive(true);
18     }
19 
20 }
相關文章
相關標籤/搜索