Yang.공부방


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
public class TestRaycastAndCollider : MonoBehaviour
{
    private int speed = 1;
    public Text txt;
    public Text txthitpoint;
    public GameObject heroGo;
    private Animation heroAnim;
    private Action OnMoveCompleted;
 
    public TestMyJoystick joystick;
    private Coroutine joystickRoutine;
 
    private void Awake()
    {
        Debug.Log("Awake");
    }
 
    void Start()
    {
        Debug.Log("Start");
 
        this.heroAnim = this.heroGo.GetComponent<Animation>();
 
        this.joystick.OnPointerUpHandler = () =>
         {
             Debug.Log("onPointer");
 
             StopCoroutine(this.joystickRoutine);
 
             heroAnim.Play("idle@loop");
         };
 
        this.joystick.OnPointerDownHandler = () =>
         {
             Debug.Log("DownPointer");
             if (this.joystickRoutine != null)
             {
                 StopCoroutine(this.joystickRoutine);
             }
             this.joystickRoutine = StartCoroutine(this.UpdatejoystickImpl());
         };
    }
 
    private IEnumerator UpdatejoystickImpl()
    {
        while (true)
        {
            Vector3 direction = Vector3.forward * joystick.Vertical + Vector3.right * joystick.Horizontal;
            if (direction != Vector3.zero)
            {
                heroAnim.Play("run@loop");
                this.heroGo.transform.position += direction * 3.0f * Time.deltaTime;
 
                var rad = Mathf.Atan2(direction.x, direction.z);
                var degree = rad * Mathf.Rad2Deg;
 
                heroGo.transform.rotation = Quaternion.LookRotation(direction);
            }
            else
            {
                heroAnim.Play("idle@loop");
            }
            yield return null;
        }
    }
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
 
public class MyJoyStick : VariableJoystick
{
    public System.Action OnPointerUpCompleted;
    public System.Action OnPointerDownCompleted;
 
    public override void OnPointerUp(PointerEventData eventData)
    {
        base.OnPointerUp(eventData);
        this.OnPointerUpCompleted();
    }
 
    public override void OnPointerDown(PointerEventData eventData)
    {
        base.OnPointerDown(eventData);
        this.OnPointerDownCompleted();
    }
}
 
cs



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

[Unity자습서] Space Shoot 해부 (배경)  (0) 2019.05.02
Move,Rotation,Shooting  (0) 2019.05.01
버튼 누르면 공격 (반복) 및 데미지Text  (0) 2019.04.25
[unity] Awake 와 Start 의 차이  (0) 2019.04.24
C#[delegate]대리자  (0) 2019.04.16