수업 022 (05.08 2020) Character Move(App.cs, Hero.cs)
2020. 5. 8. 12:00
728x90
>>> 상태변화 enum Type을 이용
App.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.UI;
public class App : MonoBehaviour
{
public Hero hero;
public Button btn;
private Vector3 targetPosition;
private void Start()
{
this.targetPosition = new Vector3(0, 0, 2);
this.btn.onClick.AddListener(() =>
{
this.hero.MoveDir = Vector3.forward;
this.hero.SetTargetPosition(this.targetPosition);
this.hero.ChangeState(Hero.eState.Run);
});
}
}
|
cs |
Hero.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
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : MonoBehaviour
{
public enum eState
{
Idle, Run
}
public Animation anim;
public eState state;
public float speed;
public Vector3 MoveDir { get; set; } // Property
private bool isRun;
private Vector3 targetPosition;
public void Run()
{
this.PlayAnimation("run@loop");
this.isRun = true;
}
public void Idle()
{
this.PlayAnimation("idle@loop");
}
private void PlayAnimation(string animName)
{
this.anim.Play(animName);
}
public void ChangeState(eState state)
{
Debug.LogFormat("{0} ---> {1}", this.state, state);
if (this.state != state)
{
this.state = state;
switch (this.state)
{
case eState.Idle:
this.Idle();
break;
case eState.Run:
this.Run();
break;
}
}
}
public void SetTargetPosition(Vector3 targetPosition)
{
this.targetPosition = targetPosition;
}
private void Update()
{
if (this.isRun)
{
var displacement = this.MoveDir * this.speed * Time.deltaTime;
this.transform.Translate(displacement);
var distance = Vector3.Distance(this.transform.position, this.targetPosition);
if (distance <= 0.02f)
{
this.ChangeState(eState.Idle);
}
}
}
}
|
cs |
>> 벡터이용 움직이기
App.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class App : MonoBehaviour
{
public GameObject hero;
public GameObject monster;
public Button btn;
public Animation anim;
public float speed;
public bool isMove;
private void Start()
{
btn.onClick.AddListener(() =>
{
this.anim = this.hero.GetComponent<Animation>();
this.anim.Play("run@loop");
this.isMove = true;
});
}
private void Update()
{
Vector3 c = this.monster.transform.position - this.hero.transform.position;
// c.magnitude, c.normalized
if (this.isMove == true)
{
Vector3 dir = c.normalized;
Vector3 displacement = this.speed * dir * Time.deltaTime;
this.hero.transform.Translate(displacement);
}
if (c.magnitude <= 0.03f)
{
this.isMove = false;
this.anim.Play("idle@loop");
}
}
}
|
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
29
30
31
32
33
34
35
36
37
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class App : MonoBehaviour
{
public Button btn;
public GameObject heroPrefab;
public GameObject weaponPrefab;
private void Start()
{
btn.onClick.AddListener(() =>
{
var heroModelGo = this.CreateModel();
var weaponGo = this.CreateWeapon();
var dummyRHand = heroModelGo.GetComponent<HeroModel>().dummyRHand;
weaponGo.transform.SetParent(dummyRHand, false);
});
}
private void Update()
{
}
private GameObject CreateModel()
{
return Instantiate(this.heroPrefab);
}
private GameObject CreateWeapon()
{
// GameObjext 변수에 attach 'Go'
return Instantiate(this.weaponPrefab);
}
}
|
cs |
728x90
'Unity > Study' 카테고리의 다른 글
수업 021 (05.07 2020) prefab (0) | 2020.05.08 |
---|---|
수업 022 (05.08 2020) 벡터 (0) | 2020.05.08 |
수업 022 (05.08 2020) MonoBehaviour(OnEnable,OnDiable) (0) | 2020.05.08 |
수업 021 (05.07 2020) Create Chars(Instantiate, SetParent) (0) | 2020.05.07 |
수업 021 (05.07 2020) Character Running til target (0) | 2020.05.07 |