상세 컨텐츠

본문 제목

Unity Study_003 5Run

C#/수업내용

by McRobbin 2020. 5. 8. 17:52

본문

5Run.unitypackage
0.05MB

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
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
 
public class Hero : MonoBehaviour
{
    public enum eState
    {
        Idle,
        Run,
        Tumbling,
        Die
    }
 
    public string name;
    public float speed;
    public eState state;
    public bool isGoal;
 
    private Animation anim;
 
    public void SetState(eState state)
    {
        this.state = state;
    }
    private void Start()
    {
        this.anim = this.gameObject.GetComponent<Animation>();
        this.state = eState.Idle;
    }
 
    private void Update()
    {
        switch (this.state)
        {
            case eState.Idle:
                anim.Play("idle@loop");
                break;
 
            case eState.Run:
                this.transform.Translate(this.transform.forward * this.speed * Time.deltaTime);
                anim.Play("run@loop");
                break;
 
            case eState.Tumbling:
                this.anim.Play("tumbling");
                break;
 
            case eState.Die:
                this.anim.Play("die");
                break;
        }
    }
}
 
 
cs

Hero클래스

 

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
100
101
102
103
104
105
106
107
108
109
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
 
public class App : MonoBehaviour
{
    public List<Hero> heroList;
    public Button createBtn;
    public Text scoreText;
 
    private float margin;
    public string[] names;
 
    private bool isStart;
    private int grade;
    private Vector3 goal;
    private void Start()
    {
        this.goal = new Vector3(005);
        margin = -2.0f;
        this.heroList = new List<Hero>();
 
        //프리팹 로드 (Hero, ch_01_01).
        //Hero 를 Instantiate 한다.
        //Hero 컴포넌트를 부착.
        //Model(ch_01_01)프리팹을 Instantiate한다.
        //Hero의 자식으로 ch_01_01을 설정.
 
        createBtn.onClick.AddListener(() =>
        {
            this.scoreText.text = "";
 
            if (heroList.Count >= names.Length)
            {
                this.isStart = true;
                foreach(var hero in heroList)
                {
                    hero.GetComponent<Animation>().Play("run@loop");
                    hero.SetState(Hero.eState.Run);
                }
            }
            else
            {
                var hero = Instantiate(Resources.Load<GameObject>("Prefabs/Hero"));
                hero.transform.position = new UnityEngine.Vector3(margin++00);
                var heroGo = Instantiate(Resources.Load<GameObject>("Prefabs/ch_01_01"));
                var heroComp = heroGo.AddComponent<Hero>();
                heroGo.transform.SetParent(hero.transform, false);
 
                heroComp.name = names[heroList.Count];
                heroComp.speed = (float)((new System.Random()).NextDouble() + 0.5);
                heroComp.SetState(Hero.eState.Idle);
                this.heroList.Add(heroComp);
            }
        });
    }
 
    private void FixedUpdate()
    {
        if (this.isStart)
        {
            foreach (var hero in heroList)
            {
                if(hero.transform.position.z > goal.z && !hero.isGoal)
                {
                    switch (this.grade)
                    {
                        case 0:
                            hero.SetState(Hero.eState.Tumbling);
                            hero.isGoal = true;
                            this.grade++;
                            this.scoreText.text += string.Format("{0}등 : {1}\n"this.grade, hero.name);
                            break;
 
                        case 1:
                        case 2:
                            hero.SetState(Hero.eState.Idle);
                            hero.isGoal = true;
                            this.grade++;
                            this.scoreText.text += string.Format("{0}등 : {1}\n"this.grade, hero.name);
                            break;
 
                        case 3:
                        case 4:
                            hero.SetState(Hero.eState.Die);
                            hero.isGoal = true;
                            this.grade++;
                            this.scoreText.text += string.Format("{0}등 : {1}\n"this.grade, hero.name);
                            break;
 
                        default:
                            break;
                    }
                }
            }
 
            if(heroList.Where(x => x.isGoal).ToList().Count == heroList.Count)
            {
                this.isStart = false;
                this.scoreText.gameObject.SetActive(true);
            }
        }
    }
}
 
 
cs

 

App.cs

관련글 더보기