01 Animation Layer
✅ 실습 진행
1. SampleScene 이름을 #01_AnimationLayer 로 변경
2. Main Camera의 위치를 Position(0, 1, 2) 로 설정
3. unitychan FBX 모델을 Hierarchy View 로 Drag & Drop
4. Animator Controller를 생성하고 , unitychan 게임오브젝트에 등록
5. unitychan 오브젝트 컴포넌트로 설정
AnimationLayer 의 Base Layer 애니메이션 등록하고 게임을 실행한다면 등록한 애니메이션 즉, 걷는 애니메이션이 실행되는 것을 확인할 수 있다.
다음으로 상체 부분만 다른 애니메이션을 적용시키기 위한 실습을 진행해보자.
이 부분은 Avatar Mask를 활용하여 만들어줄 수 있다. 아래와 같이 하체부분에 해당하는 애니메이션이 재생되지 않도록 하체 부분만 연결을 해제할 수 있다.
다음으로 AnimationLayer 의 새로운 레이어를 생성하고 설정해줄 수 있다.
1. “+” 를 눌러 새로운 레이어 생성 후 이름을 “Upper 로 설정한다
2. 설정 버튼을 눌러 Weight, Mask 를 설정한다
3. Upper Layer에 “unitychan_WAIT01” 애니메이션을 등록한다
3번 단계까지 설정을 완료한 후 게임을 실행해보면 하체는 '걷기', 상체는 '대기' 애니메이션이 실행되고 있는 순간을 확인할 수 있다.
02 Blend Tree: 1D
✅ 실습 진행
1. 새로운 씬을 생성하고 , 02_BlendTree_ 1D 으로 씬 저장
2. Main Camera의 위치를 Position(0, 1, 2) 로 설정
3. unitychan FBX 모델을 Hierarchy View 로 Drag & Drop
4. Animator Controller 를 생성하고 , unitychan 게임오브젝트에 등록
✅ 블렌드 트리 설정
1. Blend Type을 1D 로 설정
2. 생성되어 있는 Parameters 의 이름을 moveSpeed 로 설정하고 , Blend Tree 의 Parameter 에 등록
3. Add Motion으로 3 개의 Motion 생성
4. Assets - unity chan! - Art - Animations 폴더의 애니메이션을 Motion 에 등록
✅ 단축기 등록 진행
아래와 같이 Input Manager에서 단축기 등록을 진행할 수 있다
다음으로 우리 PlayerVonyroller1D.cs 스크립트를 생성하여 아래와 같이 코드를 작성해줄 수 있다.
📌 PlayerVonyroller1D.cs
using UnityEngine; public class Playercontroller1D : MonoBehaviour { private Animator animator; // private float walkSpeed = 4.0f; // private float runSpeed = 8.0f; private void Awake() { animator = GetComponent<Animator>(); } private void Update() { float vertical = Input.GetAxis("Vertical"); // 위, 아래 방향키 입력 float offset = 0.5f + Input.GetAxis("Sprint") * 0.5f; float moveParameter = Mathf.Abs(vertical * offset); animator.SetFloat("moveSpeed", moveParameter); //이동속도: Shift키를 안눌럿을 땐 walkSpeed, Shift키를 눌렀을 땐 runSpeed값이 moveSpeed에 저장 // float moveSpeed = Mathf.Lerp(walkSpeed, runSpeed, Input.GetAxis("Sprint")); // 실제 이동 // transform.position += new Verctor3(vertical, 0, 0) * moveSpeed * Time.deltaTime; } }
위와 같이 작성한 스크립트를 컴포넌트에 추가하고 게임을 실행하면 방향키를 눌렀을 경우, 걷기가 실행되고 Shift 키를 함께 누르면 뛰는 애니메이션이 제작된 것을 확인할 수 있다.
03 Blend Tree: 2D Simple Directional
✅ 실습 진행
1. 새로운 씬을 생성하고 , #03_BlendTree_2DSimpleDirectional 으로 씬 저장
2. Main Camera의 위치를 Position(0, 1, 2) 로 설정
3. unitychan FBX 모델을 Hierarchy View 로 Drag & Drop
4. Animator Controller 를 생성하고, unitychan 게임오브젝트에 등록
✅ 블렌드 트리 생성 및 설정 방법
1. Blend Type을 2D Simple Directional 로 설정
2. Parameters 2개 이름을 “Horizontal”, “Vertical"로 설정하고 , Blend Tree의 Parameter에 등록
3. Add Motion으로 5 개의 Motion 생성
4. Assets - unity chan! - Art Animations 폴더의 애니메이션을 Motion 에 등록
다음으로는 애니메이션 제어를 위해 스크립트 코드를 다음과 같이 작성해줄 수 있다.
📌 PlayerController2DSimple.cs
using UnityEngine; public class PlayerController2DSimple : MonoBehaviour { private Animator animator; private void Awake() { animator = GetComponent<Animator>(); } private void Update() { float horizontal = Input.GetAxis("Horizontal"); // 좌, 우 방향키 입력 float vertical = Input.GetAxis("Vertical"); // 위, 아래 방향키 입력 // horizontal 값에 따라 애니메이션 재생 (-1: 왼쪽, 0: 가운데, 1: 오른쪽) animator.SetFloat("Horizontal", horizontal); // vertical 값에 따라 애니메이션 재생 (-1: 뒤, 0: 가운데, 1: 앞) animator.SetFloat("Vertical", vertical); // 이동속도 // float moveSpeed = 5.0f; // 실제 이동 // transform.position += new Vector3(horizontal, 0, vertical) * moveSpeed * Time.deltaTime; } }
04 Blend Tree: 2D Freeform Directional
✅ 실습 진행
1. 새로운 씬을 생성하고 , #04_BlendTree_2DFreeformDirectional 으로 씬 저장
2. Main Camera의 위치를 Position(0, 1, 2) 로 설정
3. unitychan FBX 모델을 Hierarchy View 로 Drag & Drop
4. Animator Controller 를 생성하고, unitychan 게임오브젝트에 등록
✅ 블렌드 트리 생성 및 설정
1. Blend Type을 2D Freeform Directional 로 설정
2. Parameters 2개 이름을 “Horizontal”, “ 로 설정하고 , Blend Tree 의 Parameter 에 등록
3. Add Motion으로 9 개의 Motion 생성
4. Assets - unity chan! - Art Animations 폴더의 애니메이션을 Motion에 등록
위 설정한 내용을 바탕으로 다음과 같이 애니메이션 제어를 위한 스크립트 코드를 작성할 수 있다.
📌 PlayerController2DFreeformDirectional.cs
using UnityEngine; public class PlayerController2DFreeformDirectional : MonoBehaviour { private Animator animator; private void Awake() { animator = GetComponent<Animator>(); } private void Update() { float horizontal = Input.GetAxis("Horizontal"); // 좌, 우 방향키 입력 float vertical = Input.GetAxis("Vertical"); // 위, 아래 방향키 입력 // shift 키를 안누르면 최대 0.5, shift 키를 누르면 최대 1까지 값이 바뀌게 된다 float offset = 0.5f + Input.GetAxis("Sprint") * 0.5f; // horizontal 값에 따라 애니메이션 재생 (-1: 왼쪽, 0: 가운데, 1: 오른쪽) animator.SetFloat("Horizontal", horizontal * offset); // vertical 값에 따라 애니메이션 재생 (-1: 뒤, 0: 가운데,1: 앞) animator.SetFloat("Vertical", vertical * offset); // 이동속도 : shift키를 안눌렀을 땐 walkSpeed, shift키를 눌렀을 땐 runSpeed값이 moveSpeed에 저장 // float moveSpeed = Mathf.Lerp(walkSpeed, runSpeed, Input.GetAxis("Sprint")); // 실제이동 // transform.position += new Vector3(horizontal, 0, vertical) * moveSpeed * Time.deltaTime; } }
05 Blend Tree: 2D Freeform Cartesian
✅ 실습 진행
1. 새로운 씬을 생성하고 , #05_BlendTree_2DFreeformCartesian 으로 씬 저장
2. Main Camera의 위치를 Position(0, 1, 2)로 설정
3. unitychan FBX 모델을 Hierarchy View 로 Drag & Drop
4. Animator Controller를 생성하고 , unitychan 게임오브젝트에 등록
✅ 블렌드 트리 생성 및 설정
1. Blend Type을 2D Freeform Cartesian 으로 설정
2. Parameters 2개 이름을 “Horizontal”, “ 로 설정하고 , Blend Tree 의 Parameter 에 등록
3. Add Motion으로 4 개의 Motion 생성
4. Assets - unity chan! - Art Animations 폴더의 애니메이션을 Motion에 등록
위와 같이 설정을 완료한 후, 애니메이션 제어를 위한 스크립트 코드를 아래와 같이 작성해볼 수 있다.
📌 PlayerController2DFreeformCartesian.cs
using UnityEngine; public class PlayerController2DFreeformCartesian : MonoBehaviour { private Animator animator; private void Awake() { animator = GetComponent<Animator>(); } private void Update() { float horizontal = Input.GetAxis("Horizontal"); // 좌, 우 방향키 입력 float vertical = Input.GetAxis("Vertical"); // 위, 아래 방향키 입력 animator.SetFloat("Horizontal", horizontal); animator.SetFloat("Vertical", vertical); } }
06 Blend Tree: Direct
✅ 실습 진행
1.새로운 씬을 생성하고 , #06_Direct로 씬 저장
2. Main Camera의 위치를 Position(0, 1.3, 0.5) 로 설정
3. unitychan FBX 모델을 Hierarchy View 로 Drag & Drop
4. Animator Controller 를 생성하고, unitychan 게임오브젝트에 등록
✅ Animator Controller 설정
▶ “Direct"의 Base Layer 애니메이션 등록
✅ Avatar Mask 생성 및 설정
→ Project View - “+” - Avatar Mask에서 Avatar Mask를 생성하고 설정할 수 있다.
이때, Use skeleton from에 현재 모델로 사용하는 unitychan 의 아바타를 등록하고 , “Import skeleton” 을 눌러 설정한다. 그리고 얼굴 부위만 제어할 수 있게 바꿔야 하기 때문에 Transform을 이용해 설정할 수 있다.
✅ 블렌드 트리 생성 및 설정 방법
1. Blend Type을 Direct 로 설정
2. 4개의 표정 모션을 사용하기 때문에 Parameters 를 4 개 생성 (angry, eye, sap, smile)
3. Add Motion으로 4개의 Motion 을 생성하고, Parameter 등록
다음으로 설정한 부분을 바탕으로 애니메이션 제어를 위해 아래와 같이 스크립트 코드를 작성해줄 수 있다.
📌PlayerControllerDirect.cs
using System.Collections; using UnityEngine; public class PlayerControllerDirect : MonoBehaviour { private Animator animator; private void Awake() { animator = GetComponent<Animator>(); } private void Update() { KeyEvent(0, KeyCode.Q, "angry"); // Q키를 누르면 angry 파라미터 값 증가 KeyEvent(1, KeyCode.A, "angry"); // A키를 누르면 angry 파라미터 값 감소 KeyEvent(0, KeyCode.W, "eye"); // W키를 누르면 eye 파라미터 값 증가 KeyEvent(1, KeyCode.S, "eye"); // S키를 누르면 eye 파라미터 값 감소 KeyEvent(0, KeyCode.E, "sap"); // E키를 누르면 sap 파라미터 값 증가 KeyEvent(1, KeyCode.D, "sap"); // D키를 누르면 sap 파라미터 값 감소 KeyEvent(0, KeyCode.R, "smile"); // R키를 누르면 smile 파라미터 값 증가 KeyEvent(1, KeyCode.F, "smile"); // F키를 누르면 smile 파라미터 값 감소 } private void KeyEvent(int type, KeyCode key, string parameter) { if (Input.GetKeyDown(key)) { string coroutine = type == 0 ? "ParameterUp" : "ParameterDown"; StartCoroutine(coroutine, parameter); } else if (Input.GetKeyUp(key)) { string coroutine = type == 0 ? "ParameterUp" : "ParameterDown"; StopCoroutine(coroutine); } } private IEnumerator ParameterUp(string parameter) { float percent = animator.GetFloat(parameter); while (percent < 1) { percent += Time.deltaTime; // percent 값 증가 animator.SetFloat(parameter, percent); yield return null; } } private IEnumerator ParameterDown(string parameter) { float percent = animator.GetFloat(parameter); while (percent > 0) { percent -= Time.deltaTime; // percent 값 감소 animator.SetFloat(parameter, percent); yield return null; } } }
'Study > Unity' 카테고리의 다른 글
[Unity 3D Basic] Terrain Map (0) | 2024.06.05 |
---|---|
[Unity 3D Basic] 3D Animations / Simple Combat (0) | 2024.05.29 |
[Unity 3D Basic] Animation Layer, Blend Tree (0) | 2024.05.22 |
[Unity 3D Basic] 3D Model / Animations (0) | 2024.05.15 |
[Unity 3D Basic] Navigation Mesh 응용 (0) | 2024.05.15 |