애니메이터 설명.
애니메이터를 만들고 세팅하는 방법은 많이 있는데 코드에서 써먹는 방법이 많이 안나와 있어서 적습니다.
뇌피셜이 가득 들어가 있음을 알립니다.
위의 밴딧 모델의 애니메이터를 열었습니다.
왼쪽 파라미터에 각종 애니메이터 상태들이 있습니다.
AnimState, Attack, Recover, Jump등등,.,,
자세히 보면 오른쪽에 입력칸과 토글? 불리언 체크 하는 곳이 보입니다. 이걸로 애니메이터 상태 세팅을 어떻게 해야할지 알 수 있습니다.
Animator anim = model.GetComponent<Animator>(); 사용, 애니메이터를 받아왔습니다.
1. 동그란 토글.
동그란 토글은 아무 변수도 받지 않습니다.
anim.SetTrigger("상태 이름");
이렇게 사용하면 되겠습니다.
메서드 기능은 Animation 컴포넌트의 Play와 같습니다. 다만 상태의 전이가 있어 제약이 있습니다.
상태 이름을 적을 때 애니메이션 이름이나 저 애니메이터 블록 이름을 넣는 것이 아닙니다!
왼쪽 파라미터의 이름을 적어주어야 합니다.
2. 불리언 체크박스.
불리언 체크박스는 불리언 매개변수를 받아 상태가 전이되는 애니메이터 입니다.
anim.SetBool("상태 이름", true or false);
로 사용합니다.
세팅에 들어간 value값은 애니메이터의 상태 설정에 사용됩니다.
3. 숫자 텍스트박스.
숫자 텍스트 박스는 숫자를 입력받아 상태를 전이합니다.
anim.SetInteger("상태 이름", 매개변수);
이렇게 적어주면 됩니다.
매개변수는 상태 설정에 사용됩니다.
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test1 : MonoBehaviour
{
public Button btn;
public Button jumpBtn;
public GameObject prefab;
public Hero hero;
private void Start()
{
this.btn.onClick.AddListener(() =>
{
var go = Instantiate<GameObject>(this.prefab);
go.transform.SetParent(this.transform, false);
var heroCompo = go.GetComponent<Hero>();
this.hero = heroCompo;
heroCompo.coroutine = heroCompo.Gravity();
StartCoroutine(heroCompo.coroutine);
this.btn.gameObject.SetActive(false);
});
this.jumpBtn.onClick.AddListener(() =>
{
if(this.hero != null && this.hero.isWakeUp)
{
if (this.hero.coroutine != null)
StopCoroutine(this.hero.coroutine);
this.hero.coroutine = this.hero.Jump();
StartCoroutine(this.hero.coroutine);
}
});
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red, 1.0f);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100f))
{
if (this.hero != null && this.hero.isWakeUp)
{
if (this.hero.coroutine != null)
StopCoroutine(this.hero.coroutine);
this.hero.coroutine = this.hero.Move(hit.point);
StartCoroutine(this.hero.coroutine);
}
}
}
}
}
|
cs |
Test1.cs 메인 스크립트 입니다.
생성버튼 클릭시 캐릭터를 동적 생성하고 캐릭터가 떨어지는 코루틴 실행합니다.
버튼 누르면 해당 버튼은 나타나지 않게 했습니다.
캐릭터가 땅에 떨어지면 넘어졌다 일어나게 했는데 일어난 후에(isWakeUp == true)
무브, 점프를 실행할 수 있습니다.
점프 버튼은 히어로가 실행중인 코루틴을 중단하고 점프 코루틴을 실행하게 합니다.
업데이트에서는 사용자 마우스 입력받고 무브 코루틴 실행하게 했습니다.
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Hero : MonoBehaviour
{
public IEnumerator coroutine;
private Animator anim;
public UnityAction onJumpHighest;
public bool isGround;
public bool isWakeUp;
private void Awake()
{
this.anim = this.GetComponent<Animator>();
this.isGround = false;
this.isWakeUp = false;
}
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent<Collider>().name == "ground")
{
Debug.Log("onTrigger" + "Enter");
this.transform.position = new Vector3(this.transform.position.x, 0f);
this.isGround = true;
if (!this.isWakeUp)
{
if (this.coroutine != null)
StopCoroutine(this.coroutine);
this.coroutine = this.Chulpueouck();
StartCoroutine(this.coroutine);
}
}
}
private void OnTriggerStay(Collider other)
{
Debug.Log("trigger stay : " + other.gameObject.name);
}
private void OnTriggerExit(Collider other)
{
this.isGround = false;
}
public IEnumerator Move(Vector3 targetPos)
{
var target = new Vector3(targetPos.x, this.transform.position.y);
if (target.x - this.transform.position.x > 0.0f)
this.transform.localScale = new Vector3(-4, this.transform.localScale.y, this.transform.localScale.z);
else
this.transform.localScale = new Vector3(4, this.transform.localScale.y, this.transform.localScale.z);
this.anim.SetInteger("AnimState", 2);
while(Vector3.Distance(this.transform.position, target) > 0.03f)
{
this.transform.position += (target - this.transform.position).normalized * Time.deltaTime * 1.0f;
yield return null;
}
this.anim.SetInteger("AnimState", 0);
}
public IEnumerator Jump()
{
this.anim.SetBool("Grounded", false);
this.anim.SetTrigger("Jump");
float speed = 13.0f;
while(speed > 0)
{
this.transform.position += new Vector3(0, speed) * Time.deltaTime;
speed -= 0.3f;
yield return null;
}
yield return StartCoroutine(this.Gravity());
this.anim.SetBool("Grounded", true);
this.anim.SetInteger("AnimState", 0);
}
public IEnumerator DestroyHero()
{
yield return new WaitForSeconds(2.0f);
Destroy(this.gameObject);
}
public IEnumerator Gravity()
{
float speed = 0f;
this.anim.SetTrigger("Jump");
while (!this.isGround)
{
speed += 0.3f;
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y - Time.deltaTime * speed);
yield return null;
}
}
public IEnumerator Chulpueouck()
{
this.anim.SetTrigger("Death");
yield return new WaitForSeconds(1.0f);
this.anim.SetTrigger("Recover");
yield return new WaitForSeconds(1.0f);
this.anim.SetInteger("AnimState", 0);
this.isWakeUp = true;
}
}
|
cs |
Hero.cs 입니다.
저는 OnTriggerXXX함수 사용했구요 콜라이더가 통과할수 있게(콜라이더 접촉시 물리적용 안받게) 구현했습니다.
캐릭터 여러개 생성시 겹쳐집니다.
그래서 중력도 끄고 했구요 중력 적용받는 함수를 따로 코루틴으로 만들었습니다.
Move 함수는 코루틴 매프레임 기다리면서 실행하게 했구요, 최초 코루틴 실행시 현재 캐릭터와 찍은 좌표의 벡터
방향을 보고 캐릭터가 바라볼 방향을 정했습니다.
그 이후에는 매프레임 x축만 이동하게 했습니다.
Jump 함수는 코루틴 내에서 코루틴을 호출했습니다. 근데 그냥 호출한건 아니고 yield return으로 호출해서
내가 부른 코루틴이 종료될때 까지 기다렸다가 다시 진행하던 코루틴을 진행하게 했습니다.
위로 속도값을 줘서 올라가다가 올라가는 속도가 0보다 작아지면 아까 구현해놓은 중력을 받게 하는 코루틴을 부르는 것으로 했습니다.
Culpueouck 함수는 오타가 났는데;; 철푸덕 함수 입니다. 저는 처음 캐릭터 생성시 떨어지면서 땅에 닿으면 한번
넘어졌다 일어나게 했습니다. 물론 이 동안에 움직이거나 점프는 불가능 하구요.
애니메이터 조지는 yield덩어리 코루틴 입니다.
Unity 쿠키런 용과쿠키. (0) | 2020.05.29 |
---|---|
Unity Study_015 HudText, Coroutine (0) | 2020.05.26 |
Unity Study_013-2 마우스 클릭 좌표 이동 (0) | 2020.05.26 |
Unity Study_013-1 로그인 구현 (0) | 2020.05.22 |
Unity Study_007 무기변경, 닭 잡기. (0) | 2020.05.14 |