C#/수업내용

2020.05.07. 수업내용 - Unity 버튼, 이동, 애니메이션 실행

dev_sr 2020. 5. 8. 00:10

 

1-1. 버튼을 누르면 공격 애니메이션을 실행하고

공격하는 프레임에 맞춰서 "타격"이 출력

끝나면 idle loop애니메이션 실행

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
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;
 
public class App : MonoBehaviour
{
    public GameObject model;
    public Button btn;
    public Animation anim;
    public bool attack;
    public bool attackEnd;
    public float timeCompareAttack;
    public float timeCompareAttackEnd;
    public float result;
    public AnimationState state;
 
    void Start()
    {
        this.anim = this.model.GetComponent<Animation>();
        state = anim["attack_sword_01"];
        Debug.LogFormat("state.length {0}", state.length); //0.73333
        Debug.LogFormat("state.length {0}", state.clip.frameRate); //30
 
        float totalFrame = state.length * state.clip.frameRate;
 
        result = 8 * state.length / totalFrame;
 
 
        this.btn.onClick.AddListener(() =>
        {
            Debug.Log("공격 시작");
            this.anim.Play("attack_sword_01");  
            this.attack = true;
            this.attackEnd = true;
        });
   
    }
 
    private void Update()
    {
        if(this.attack==true)
        {
            this.timeCompareAttack += Time.deltaTime;
        }
        if(this.attackEnd==true)
        {
            this.timeCompareAttackEnd += Time.deltaTime;
            this.Attack();
        }
    }
 
    private void Attack()
    {
        if(this.result<=this.timeCompareAttack)
        {
            Debug.Log("타격");
            this.timeCompareAttack = 0;
            this.attack = false;
        }
 
        if(this.state.length <=this.timeCompareAttackEnd)
        {
            Debug.Log("공격 종료");
            this.anim.Play("idle@loop");
            this.timeCompareAttackEnd = 0;
            this.attackEnd = false;
        }
 
    }
 
}

 

 

1-2. 결과값

1)idle 상태

 

2)  버튼을 누르면 타격하는 프레임 나올 때 타격 출력

Window->Animation->Animation

 

 

 

2-1.  전진 후진 왼쪽 오른쪽 정지 버튼 만들고 이동시키거나 정지시키기

이동할때는 달리는 모션이 실행되고 정지되면 idle모션이 실행된다.

 

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
 
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.Timeline;
using UnityEngine.UI;
 
public class App : MonoBehaviour
{
    public GameObject model;
    public Animation anim;
    public AnimationState state;
    public Button upbtn;
    public Button downbtn;
    public Button leftbtn;
    public Button rightbtn;
    public Button pausebtn;
    public bool isMove;
    public Vector3 direction;
 
 
    void Start()
    {
        this.anim = this.model.GetComponent<Animation>();
       
        this.upbtn.onClick.AddListener(() =>
        {
            this.Move(Vector3.forward);
        });
        this.downbtn.onClick.AddListener(() =>
        {
            this.Move(Vector3.back);
        });
        this.leftbtn.onClick.AddListener(() =>
        {
            this.Move(Vector3.left);
        });
        this.rightbtn.onClick.AddListener(() =>
        {
            this.Move(Vector3.right);
        });
        this.pausebtn.onClick.AddListener(() =>
        {
            this.Stop();
        });
 
    }
 
    private void Update()
    {
        
        if (this.isMove)
        {
            float speed = 1.2f;
            this.anim.gameObject.transform.Translate(this.direction * speed * Time.deltaTime);
        }
 
    }
 
 
 
    private void Move(Vector3 direction)
    {
        this.direction = direction;
        this.anim.Play("run@loop");
        this.isMove = true;
    }
 
    private void Stop()
    {
        this.isMove = false;
        this.anim.Play("idle@loop");
    }
}

 

2-2. 결과값

1) 오른쪽 버튼 눌렀을 때 화면상 왼쪽(카메라 180도 회전)으로 뛰는 모습

 

2) 위 버튼을 눌렀을 때 앞으로 전진하는 모습