2D Running Game 기본 시스템 만들기

Unity/과제 2019. 5. 9. 19:05



수정해야 할 것 : UI 점수 갱신, 점프 모션 어색함 완화


1
2
3
4
5
6
7
8
9
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
 
public class TestPlayer : MonoBehaviour
{
    public Animator anim;
 
    private void Awake()
    {
        this.anim = this.gameObject.GetComponent<Animator>();
        this.transform.position = new Vector3(-1.351f, 0.309f, 0);
    }
 
    private void Start()
    {
        this.anim.Play("fox_run");
    }
 
    public void Jump()
    {
        StartCoroutine(this.JumpImpl());
    }
 
    private IEnumerator JumpImpl()
    {
        this.anim.Play("fox_jump");
        this.transform.DOJump(this.transform.position, 0.3f, 10.5f);
        yield return new WaitForSeconds(this.anim.GetCurrentAnimatorStateInfo(0).length);
        this.anim.Play("fox_run");
    }
 
    private void OnTriggerEnter2D(Collider2D other)
    {
        other.gameObject.SetActive(false);
        var screenpos = Camera.main.WorldToScreenPoint(other.transform.position);
        var gemUiGo = Instantiate(Resources.Load<GameObject>("CherryImage"));
        gemUiGo.transform.SetParent(GameObject.Find("Canvas").transform, false);
        gemUiGo.transform.position = screenpos;        
        gemUiGo.transform.DOMove(GameObject.Find("CherryImage").transform.position, 0.5f).OnComplete(() =>
        {
            Destroy(gemUiGo);
        });
    }
}
 
cs


1
2
3
4
5
6
7
8
9
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class TestSunnyLand : MonoBehaviour
{
    public GameObject backGround;
    public GameObject way;
    public GameObject player;
    public GameObject gemLayer;
    public Button jumpBtn;
    public List<GameObject> listCherry;
 
    private void Awake()
    {
        this.backGround.transform.position = new Vector3(000);
        this.way.transform.position = new Vector3(3.392575f, 0.1069814f, -0.06640625f);
    }
 
    private void Start()
    {
        this.jumpBtn.onClick.AddListener(() =>
        {
            var playerScript = this.player.GetComponent<TestPlayer>();
            playerScript.Jump();
        });
        this.GameStart();
    }
 
    public void GameStart()
    {
        foreach(var cherry in this.listCherry)
        {
            cherry.SetActive(true);
        }
        StartCoroutine(this.MoveBackGround());
 
        StartCoroutine(this.MoveWay());
 
        StartCoroutine(this.MoveGemLayer());
    }
 
    private IEnumerator MoveBackGround()
    {
        var bg = this.backGround.transform.position;
        var originPosition = bg;
        while(true)
        {
            if (this.backGround.transform.position.x > 0.35f)
            {
                this.backGround.transform.position = originPosition;
            }
            else
            {
                this.backGround.transform.position += new Vector3(0.03f * Time.deltaTime, 00);
            }
            yield return null;
        }
    }
 
    private IEnumerator MoveWay()
    {
        var pos = this.way.transform.position;
        var originPosition = pos;
        while(true)
        {
            if(this.way.transform.position.x <= -5.5f)
            {
                this.way.transform.position = originPosition;
            }
            this.way.transform.position -= new Vector3(0.9f * Time.deltaTime, 00);
            yield return null;
        }
    }
 
    private IEnumerator MoveGemLayer()
    {
        var pos = this.gemLayer.transform.position;
        var originPosition = pos;
        while (true)
        {
            if (this.gemLayer.transform.position.x < -9.03f)
            {
                foreach (var cherry in this.listCherry)
                {
                    if (!cherry.activeSelf)
                    {
                        cherry.SetActive(true);
                    }
                }
                this.gemLayer.transform.position = originPosition;
            }
            this.gemLayer.transform.position -= new Vector3(0.9f * Time.deltaTime, 00);
            yield return null;
        }
    }
}
 
cs


'Unity > 과제' 카테고리의 다른 글

Isometric, 클릭시 좌표 출력하기  (0) 2019.05.13
씬 전환 및 가서 공격하기  (0) 2019.04.16
왔다갔다 때리기  (0) 2019.04.16
0  (0) 2019.04.11
: