'coroutine vs update'에 해당되는 글 1건

  1. 2019.11.29 11.29 캐릭터 움직이기 (Coroutine, Update 비교 및 활용)

11.29 캐릭터 움직이기 (Coroutine, Update 비교 및 활용)

C#/실습 2019. 11. 29. 16:11

Move(Coroutine)을 누른후

코드:

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
using UnityEngine;
public class App : MonoBehaviour
{
    public Button btnMoveCoroutine;
    public Button btnMoveUpdate;
    public Hero hero;
    public GameObject targetPoint;
    // Start is called before the first frame update
    void Start()
    {
        GameObject heroGO = new GameObject("Hero");
        hero = heroGO.AddComponent<Hero>();
 
        string path = string.Format("Prefabs/{0}""ch_01_01");
        var prefab = Resources.Load<GameObject>(path);
        var model = Instantiate(prefab);
        model.transform.SetParent(heroGO.transform, false);
        model.transform.localPosition = Vector3.zero;
        hero.model = model;
 
        hero.onMove = (target) =>
        {
            var dis = Vector3.Distance(hero.transform.position, target.transform.position);
            var vc = target.transform.position - hero.transform.position;
            var dir = vc.normalized;
            var speed = 1;
            hero.transform.position += dir * speed * Time.deltaTime;
            if (dis <= 0.5f)
            {
                hero.transform.position = target.transform.position;
                hero.model.GetComponent<Animation>().Play("idle@loop");
                hero.isMove = false;
            }
        };
 
        btnMoveCoroutine.onClick.AddListener(() =>
        {
            hero.Move2(targetPoint);
        });
        btnMoveUpdate.onClick.AddListener(() => {
            hero.Move(targetPoint);
        });
 
    }
 
    // Update is called once per frame
    void Update()
    {
 
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
 
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Hero : MonoBehaviour
{
    public bool isMove;
    public Action<GameObject> onMove;
    public GameObject target;
    public GameObject model;
    IEnumerator onMoveCoroutine;
    // Start is called before the first frame update
    void Start()
    {
        onMoveCoroutine = OnMoveCoroutine();
    }
 
    public void Move(GameObject target)
    {
        this.target = target;
        isMove = true;
    }
    public void Move2(GameObject target)
    {
 
        this.target = target;
        //isMove = true;
        this.model.GetComponent<Animation>().Play("run@loop");
        StartCoroutine(onMoveCoroutine);
    }
    public void Update()
    {
        if (this.isMove == true)
        {
            this.model.GetComponent<Animation>().Play("run@loop");
            this.onMove(target);
        }        
        if (this.transform.position == target?.transform.position)
        {
            StopCoroutine(onMoveCoroutine);
        }
    }
 
    IEnumerator OnMoveCoroutine()
    {
        while (true)
        {
            var dis = Vector3.Distance(this.transform.position, target.transform.position);
            var vc = target.transform.position - this.transform.position;
            var dir = vc.normalized;
            var speed = 1;
            this.transform.position += dir * speed * 0.01f;
            this.transform.LookAt(target.transform);
            if (dis <= 0.1f)
            {
                this.model.GetComponent<Animation>().Play("idle@loop");
                this.transform.position = target.transform.position;
                //isMove = false;
            }
            yield return new WaitForSeconds(0.01f);
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
: