C#/예제

WayPoint 이동, Line Renderer

dev_sr 2020. 6. 9. 17:50

캐릭터는 웨이포인트마다 이동하면서 적들을 처치한다.

적들마다 웨이포인트를 각각 다르게 어싸인 해주면 가까운 웨이포인트에 플레이어가 왔을 때

해당되는 몬스터만 달려간다

 

LineRenderer - 두 물체 사이에 라인을 그린다

 

this.lr.SetPosition(1new Vector3(000)); 

 

1-> 인덱스

new Vector3(0,0,0)  - 거리

플레이어에 붙어있는 LineRenderer  컴포넌트에서 use world space를 꺼주고

공격할 때만 거리를 다시 늘려준다

 

this.lr.SetPosition(1new Vector3(00, distance));

 

1. App

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class App : MonoBehaviour
{
    private void Awake()
    {
        DontDestroyOnLoad(this);
    }
 
    //게임 엔진 응용 프로그래밍, 2020-06-04
    void Start()
    {
        SceneManager.LoadScene("Title");
    }
 
}
 

 

 

2. Title 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
 
public class Title : MonoBehaviour
{
    public Button normalBtn;
    public Button vrBtn;
 
    void Start()
    {
        this.normalBtn.onClick.AddListener(() =>
       {
           SceneManager.LoadScene("InGame_Normal");
       });
 
        this.vrBtn.onClick.AddListener(() =>
        {
            SceneManager.LoadScene("InGame_VR");
        });
    }
  
}
 

 

 

 

3. InGame_Normal 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class InGame_Normal : MonoBehaviour
{
    public UIInGame_Normal uiInGame_Normal;
    public Player player;
    public Transform[] wayPoints;
 
    void Start()
    {
        this.uiInGame_Normal.startBtn.onClick.AddListener(() =>
        {
            this.uiInGame_Normal.HideBtn();
            player.Init(this.wayPoints);
        });
 
        this.player.OnDefeatLastTargetNotiInGame = () =>
         {
             //this.uiInGame_Normal.ShowBtn();
             SceneManager.LoadScene("Title");
         };
    }
 
   
}
 

 

 

 

4. UIInGame_Normal 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class UIInGame_Normal : MonoBehaviour
{
    public Button startBtn;
 
    public void ShowBtn()
    {
        this.startBtn.gameObject.SetActive(true);
    }
 
    public void HideBtn()
    {
        this.startBtn.gameObject.SetActive(false);
    }
 
}

 

 

5. Player 

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
 
public class Player : MonoBehaviour
{
    public Monster[] monster;
 
    private Animator anim;
    private float speed = 1.8f;
    private Transform[] wayPoints;
    private bool isStop = false;
    private int wayIndex=0;
    private int monsterIndex=0;
    private float attackRange=5f;
    private bool isLast;
    private float elapsedTime;
 
 
    public UnityAction OnMoveComplete;
    public UnityAction OnWayPoint;
    public UnityAction OnAttackCompleteNotiMonster;
    public UnityAction OnAttackComplete;
    public UnityAction OnDefeatLastTarget;
    public UnityAction OnDefeatLastTargetNotiInGame;
 
    public ParticleSystem ps;
    public LineRenderer lr;
 
    void Start()
    {
 
        this.anim = this.gameObject.GetComponent<Animator>();
 
        this.OnMoveComplete = () =>
        {
            this.StartCoroutine(this.AttackImpl());
        };
        this.OnAttackComplete = () =>
         {
             this.StartCoroutine(this.MoveImpl());
         };
 
        this.OnDefeatLastTarget = () =>
         {
             this.lr.SetPosition(1new Vector3(000));
             this.OnDefeatLastTargetNotiInGame();
             this.StopAllCoroutines();
         };
    }
 
    public void Init(Transform[] wayPoints)
    {
        Debug.Log("Playe::Init");
        this.wayPoints = wayPoints;
        this.StartCoroutine(this.MoveImpl());
    }
 
    IEnumerator MoveImpl()
    {
        
        var targetTr = this.wayPoints[this.wayIndex].transform;
        this.transform.LookAt(targetTr);
        this.anim.Play("Move");
 
        while (true)
        {
            if (!this.isStop)
            {
                this.transform.Translate(Vector3.forward * this.speed * Time.deltaTime);
                float distance = Vector3.Distance(targetTr.position, this.transform.position);
                this.lr.SetPosition(1new Vector3(000));
                if (distance<0.03f)
                {
                    this.isStop = true;
                    break;
                }
 
            }
            yield return null;
        }
        this.OnMoveComplete();
        this.Idle();
    }
 
    IEnumerator AttackImpl()
    {
        var target = this.monster[this.monsterIndex];
 
        while (true)
        {
            var distance = Vector3.Distance(target.transform.position, this.transform.position);  
            
            if(distance<=this.attackRange)
            {
                Debug.Log("공격!");
                this.ps.Play();
 
                this.lr.SetPosition(1new Vector3(00, distance));
 
                this.OnAttackCompleteNotiMonster();
 
                if (this.monsterIndex == this.monster.Length-1)
                {
                    Debug.Log("마지막 타겟 처치");
                    this.wayIndex = 0;
                    this.monsterIndex = 0;
 
                    this.isLast = true;
                }
                else
                {
                    this.wayIndex++;
                    this.monsterIndex++;
                    this.OnAttackComplete();
                    this.isStop = false;
                }
 
                break;
            }
            
            yield return null;
        }
 
        
        this.Idle();
        
    }
 
    private void Idle()
    {
        this.anim.Play("Idle");
    }
 
    private void Update()
    {
        if(this.isLast)
        {
            this.elapsedTime += Time.deltaTime;
 
            if(this.elapsedTime>=2f)
            {
                this.OnDefeatLastTarget();
                this.elapsedTime = 0;
                this.isLast = false;
            }
            
        }
 
 
    }
 
}
 
cs

 

 

6.  Monster 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
 
public class Monster : MonoBehaviour
{
    private Animator anim;
    private bool isDead;
    private bool isMove;
 
    public Player player;
    public Transform WayPoint;
 
    private void Start()
    {
        this.anim = this.gameObject.GetComponent<Animator>();
        this.StartCoroutine(this.CheckImpl());
 
        this.StartCoroutine(this.MoveImpl());
 
    }
 
    IEnumerator MoveImpl()
    {    
        
        
        while(true)
        {
            this.transform.LookAt(this.player.transform);
            if (this.isMove)
            {
                this.anim.Play("Move");
                this.transform.Translate(Vector3.forward * 1.5f * Time.deltaTime);
 
                this.player.OnAttackCompleteNotiMonster = () =>
                {
                    this.Death();
                };
                if (this.isDead)
                {
                    break;
                }
 
            }
 
            yield return null;
        }
    }
 
    IEnumerator CheckImpl()
    {
        while(true)
        {
            float distance = Vector3.Distance(this.player.transform.position, this.WayPoint.position);
 
            if(distance<=0.2f)
            {
                this.isMove = true;
            }
 
            yield return null;
        }
    }
 
    public void Death()
    {
        this.anim.Play("Death");
        this.isDead=true;
        this.isMove = false;
        
    }
 
}
 

 

 

7. AnimatorReceiver - 오류 방지용

1
2
3
4
5
6
7
8
9
10
11
12
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class AnimatorReceiver : MonoBehaviour
{
    public void StartSinking()
    {
        //애니메이션 이벤트 오류 방지용 스크립트
    }
}
 

 

 

 

'C# > 예제' 카테고리의 다른 글

문제 5  (0) 2020.04.25
문제 4  (0) 2020.04.22
문제 3  (0) 2020.04.22
문제 2  (0) 2020.04.21
문제 1  (0) 2020.04.21