<캐릭터 조작>화면 터치, WASD, 조이스틱

Unity/수업내용 2019. 4. 25. 18:22
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class TestRayCasAndCollider : MonoBehaviour
{
    public Text txt;
    public Text txtHitPoint;
 
    public GameObject heroGo;
    private Animator heroAnim;
    private float speed = 2;
 
    //조이스틱
    public TestMyJoyStick joystick;
 
    private Coroutine joystickRoutine;
 
    void Awake()
    {
        Debug.Log("어우에읶끄");
        this.heroAnim = this.heroGo.GetComponent<Animator>();
 
    }
 
    void Start()
    {
        Debug.Log("레이케스트 콜라이더 테스트체잿시뱆ㄷ가사");
 
        joystick.OnPointerDownHandler = () =>
        {
            Debug.Log("따따운핸들럴");
            
            StartCoroutine(UpdateJoyStickImple());
        };
        joystick.OnPointerUpHandler = () =>
        {
            StopAllCoroutines();
            Debug.Log("어법핸들럴럴럴");
            this.heroAnim.Play("idle@loop");
        };
    }
 
    private IEnumerator UpdateJoyStickImple()
    {
        while (true)
        {
            var dir = (Vector3.forward * joystick.Vertical + Vector3.right * joystick.Horizontal).normalized;
 
            Debug.LogFormat("{0}", dir);
            if (dir != Vector3.zero)
            {
                //보는방향
                //var rad = Mathf.Atan2(dir.x, dir.z);
                //var dig = Mathf.Rad2Deg * rad;
                //this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0));
                this.heroGo.transform.rotation = Quaternion.LookRotation(dir);
 
                //움직임
                this.heroAnim.Play("run@loop");
                this.heroGo.transform.position += dir * this.speed * Time.deltaTime;
 
                yield return null;
            }
            else
            {
                this.heroAnim.Play("idle@loop");
            }
 
        }
    }
 
    private void Move(Vector3 pos)
    {
        StopAllCoroutines();
        StartCoroutine(this.MoveImpl(pos));
    }
 
    private IEnumerator MoveImpl(Vector3 pos)
    {
        this.heroAnim.Play("run@loop");
        var dir = (pos - this.heroGo.transform.position).normalized;
        var dis = Vector3.Distance(pos, this.heroGo.transform.position);
        this.heroGo.transform.LookAt(pos);
        while (true)
        {
            if (dis > 0)
            {
                this.heroGo.transform.position += dir * this.speed * Time.deltaTime;
                dis -= this.speed * Time.deltaTime;
                yield return null;
            }
            else
            {
                this.heroAnim.Play("idle@loop");
                this.heroGo.transform.position = pos;
                break;
            }
        }
 
    }
    
    //화면을 누른 좌표로 이동
    private void UpdateMouseInput()
    {
        if (Input.GetMouseButtonUp(0))
        {
            //마우스 스크린 좌표를 화면에 표시
            Debug.LogFormat("마우스 포지션 : {0}", Input.mousePosition);
            this.txt.text = Input.mousePosition.ToString();
 
            //레이 객체를 얻어옴
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
            //화면에 레이를 표시
            Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 5f);
 
            RaycastHit hitInfo;
            if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, 1000))
            {
                Debug.LogFormat("히트 포인트 : {0}", hitInfo.point);
 
                this.txtHitPoint.text = hitInfo.point.ToString();
 
                this.Move(hitInfo.point);
            }
            else
            {
                this.txtHitPoint.text = "빈 공간";
            }
        }
    }
 
    //WASD or 방향키 입력 이동
    private void UpdateKeyInput()
    {
        var keyX = Input.GetAxis("Horizontal");
        var keyY = Input.GetAxis("Vertical");
 
        Vector3 dir = new Vector3(keyX, 0, keyY).normalized;
 
        Debug.LogFormat("{0}", dir);
 
        if (dir != Vector3.zero)
        {
            //보는방향
            //var rad = Mathf.Atan2(keyX, keyY);
            //var dig = Mathf.Rad2Deg * rad;
            //this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0));
            this.heroGo.transform.rotation = Quaternion.LookRotation(dir);
 
            //움직임
            this.heroAnim.Play("run@loop");
            this.heroGo.transform.position += dir * this.speed * Time.deltaTime;
        }
        else
        {
            this.heroAnim.Play("idle@loop");
        }
    }
 
 
    private void UpdateJoyStickInput()
    {
        var dir = (Vector3.forward * joystick.Vertical + Vector3.right * joystick.Horizontal).normalized;
 
        Debug.LogFormat("{0}", dir);
 
        if (dir != Vector3.zero)
        {
            //보는방향
            //var rad = Mathf.Atan2(dir.x, dir.z);
            //var dig = Mathf.Rad2Deg * rad;
            //this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0));
            this.heroGo.transform.rotation = Quaternion.LookRotation(dir);
 
            //움직임
            this.heroAnim.Play("run@loop");
            this.heroGo.transform.position += dir * this.speed * Time.deltaTime;
        }
        else
        {
            this.heroAnim.Play("idle@loop");
        }
    }
}
 
cs


'Unity > 수업내용' 카테고리의 다른 글

(1)BGLoop  (0) 2019.05.01
LayerMask  (0) 2019.05.01
자습서1(WASD이동, 마우스 포인트 Look, 라인렌더링  (0) 2019.04.30
HUDTest  (0) 2019.04.25
<죽어라돌덩이>  (0) 2019.04.12
: