0

Unity/과제 2019. 4. 11. 22:04

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Character : MonoBehaviour
{
    private float moveSpeed = 1;
    private float distance;
    private float attackRange;
    private float translation = 0;
    private int attackDamage;
    private int hp;
    private int count = 0;
    private int attackCount = 1;
    private Vector3 normVector;
    private Vector3 targetPosition;
    private Character target;
    private Animation anim;
    private bool isMoveStart = false;
    private bool isAttackStart = false;
 
    void Awake()
    {
        this.anim = this.gameObject.GetComponent<Animation>();
    }
 
    // Start is called before the first frame update
    void Start()
    {
 
    }
 
    //초기화
    public void Init(int attackDamage, int hp, float attackRange, Vector3 initPosition)
    {
        this.attackDamage = attackDamage;
        this.hp = hp;
        this.attackRange = attackRange;
        this.gameObject.transform.position = initPosition;
    }
 
    //공격
    public void AttackMotion()
    {
        switch (this.attackCount)
        {
            case 1:
                {
                    this.anim.Play("attack_sword_01");
                }
                break;
        }
            //case 2:
            //    {
            //        this.anim.Play("attack_sword_02");
            //    }
            //    break;
            //case 3:
            //    {
            //        this.anim.Play("attack_sword_03");
            //    }
        //    //    break;
        //}
        //this.attackCount++;
        //if (this.attackCount > 3)
        //{
        //    this.attackCount = 1;
        //}
 
    }
 
    //피해받음
    public void GetDamage(int damage)
    {
        this.hp -= damage;
        Debug.Log("데미지를 입었습니다.");
    }
 
    //이동
    public void Move()
    {
        this.gameObject.transform.position += this.normVector * this.moveSpeed * Time.deltaTime;
        this.distance = Vector3.Distance(this.targetPosition, this.gameObject.transform.position);
 
        if (this.distance <= this.attackRange)
        {
            Debug.Log("이동완료");
            this.anim.Play("idle@loop");
            this.isAttackStart = true;
            this.isMoveStart = false;
        }
    }
 
    //이동시작
    public void MoveStart(Character target)
    {
        this.target = target;
        Debug.Log("이동시작");
        this.isMoveStart = true;
        this.targetPosition = this.target.gameObject.transform.position;
        this.normVector = (this.targetPosition - this.gameObject.transform.position).normalized;
 
        //animation start
        this.anim.Play("run@loop");
 
        //방향 변경
        this.gameObject.transform.LookAt(targetPosition);
    }
 
    // Update is called once per frame
    void Update()
    {
 
        this.translation += Time.deltaTime * 10;
        //Debug.Log(translation);
 
        if (isMoveStart)
        {
            this.Move();
        }
        else if (isAttackStart)
        {
            //Debug.Log(this.translation);
            if (this.attackCount == 1)
            {
                if (this.translation >= 6.48f)
                {
                    this.translation = 0;
                    this.target.GetDamage(this.attackDamage);
                }
                else if (this.translation <= 6.47f)
                {
                    this.AttackMotion();
                }
            }
        }
 
    }
}
 
cs

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class App : MonoBehaviour
{
    public Character hero;
    public Character monster;
 
    void Awake()
    {
        this.hero.Init(15700.5f, new Vector3(000));
        this.monster.Init(1050, 1f, new Vector3(505));
        this.hero.MoveStart(this.monster);
 
    }
    // Start is called before the first frame update
    void Start()
    {
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
cs


'Unity > 과제' 카테고리의 다른 글

Isometric, 클릭시 좌표 출력하기  (0) 2019.05.13
2D Running Game 기본 시스템 만들기  (0) 2019.05.09
씬 전환 및 가서 공격하기  (0) 2019.04.16
왔다갔다 때리기  (0) 2019.04.16
: