測手速小遊戲

看到傅園慧女神的洪荒之力,心中也充滿了洪荒之力,順勢做一個測手速的小遊戲,雖然做的有點渣。誰讓我既不會美術又不會動畫,圖還得自己做、動畫還得自己截,差點折騰死我。
下面展示(輕吐槽,我知道做得難看):
再說一下:資源、圖、動畫啥的就不要太認真了,我費了九牛二虎之力也只這個水平了。(PS:大部分時間就和這些槓上了,沒美工真可怕)

首先,第一個scene一張圖說明下一下:
 
這個scene的腳本我都不想貼,就這兩東西
[C#] 純文本查看 複製代碼
?
 
1
2
3
4
5
6
7
public void IStart()
    {
        SceneManager.LoadScene("Scene1");
    }
    public void IExit()
    {
        Application.Quit();
    }


第二個scene也先來一張圖,清晰明瞭:
這部分使用了兩塊代碼,分別是相機平滑跟隨player的followTarget.cs和Move.cs。
其中的第二個腳本雖然用最通俗的if來寫的,但是包含了移動的加速減速、動畫的播放停止、秒計時器、數據存儲等諸多方面,列出來看看吧。
[C#] 純文本查看 複製代碼
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
using UnityEngine;
using System.Collections;
 
public class followTarget : MonoBehaviour {
 
    public float smoothing = 3;
    private Transform player;
 
    // Use this for initialization
    void Start()
    {
        //player = GameObject.FindGameObjectWithTag(Tags.player).transform;
        player = GameObject.Find("swimmer").transform;
    }
 
    // Update is called once per frame
    void FixedUpdate()
    {
        Vector3 targetPos = player.position + new Vector3(0, 0, -6);
        transform.position = Vector3.Lerp(transform.position, targetPos, smoothing * Time.deltaTime);
    }
}


[C#] 純文本查看 複製代碼
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
 
public class Move : MonoBehaviour {
 
    public float speed = 1;
    float speed1 = 0;
    float i = 0;
    int m = 0;//時間
    int n = 0;//點擊次數
    Vector2 ToPos = new Vector2(1,1);
    GameObject player;
 
    Text indexClick;
    Text indexTime;
 
    bool ifSave = true;
    //動畫組件
    private Animator mAnim;
 
        // Use this for initialization
        void Start () {
        player = GameObject.Find("swimmer");
        indexClick = GameObject.Find("Canvas").transform.Find("clickText").GetComponent<Text>();
        indexTime = GameObject.Find("Canvas").transform.Find("timeText").GetComponent<Text>();
 
        mAnim = GetComponent<Animator>();
        mAnim.SetBool("idle", true);
        mAnim.SetBool("swim", false);
        }
         
        // Update is called once per frame
        void Update () {
 
        if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.X))
        {
            speed1 += 0.9f;
            n++;
            indexClick.text = "點 擊 次 數:" + n.ToString();
 
            mAnim.SetBool("swim", true);
            mAnim.SetBool("restart", true);
        }
        else if(speed1 > 0)
        {
            speed1 -= 0.09f;
            mAnim.SetBool("swim", false);
            mAnim.SetBool("restart", false);
        }
        else
        {
            speed1 = 0;
            mAnim.SetBool("stop", true);
        }
        GetComponent<Rigidbody2D>().MovePosition(transform.position + new Vector3(1, 0, 1) * speed1 * Time.deltaTime);
 
        //秒計時器
        if (i >= 1.0f)
        {
            i = 0;
            m++;
            indexTime.text = "用時(秒):" + m.ToString();
            //print(m);
        }
        i += Time.deltaTime;
 
        //數據存儲
        if (player.transform.position.x > 8.4f && ifSave == true)
        {
            PlayerPrefs.SetInt("indexClick", n);
            PlayerPrefs.SetInt("indexTime", m);
            ifSave = false;
            SceneManager.LoadScene("Scene2");
        }
         
        }
}


第三部分就更簡單了,取一下第二個scene存的數據,然後顯示出來就行了。本來想加個排行榜,然後想了下,這個再說吧,先不浪費時間了。
如圖:
代碼也很簡潔:
[C#] 純文本查看 複製代碼
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
 
public class showFinal : MonoBehaviour {
 
    int indexTime;
    int indexClick;
 
    Text ClickText;
    Text TimeText;
 
        // Use this for initialization
        void Start () {
        indexClick = PlayerPrefs.GetInt("indexClick");
        indexTime = PlayerPrefs.GetInt("indexTime");
        ClickText = GameObject.Find("Canvas").transform.Find("clickText").GetComponent<Text>();
        TimeText = GameObject.Find("Canvas").transform.Find("timeText").GetComponent<Text>();
        ClickText.text = "點 擊 次 數:" + indexClick.ToString();
        TimeText.text = "用時(秒):" + indexTime.ToString();
        }
         
        // Update is called once per frame
        void Update () {
         
        }
 
    public void Restart()
    {
        SceneManager.LoadScene("Scene1");
    }
    public void Exit()
    {
         Application.Quit();
    }
}

可以發佈成安卓的,也可以發佈成pc的,pc的按X鍵或者鼠標加速,小夥伴們,爆發你的洪荒之力吧(傅女神借我點洪荒之力唄)。
發佈了35 篇原創文章 · 獲贊 13 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章