[Unity] 3D Animation Layer, Blend Tree (실습)

Southbig·2023년 3월 28일
0

Avata Mask

오브젝트 생성 및 Animator Controller 적용

오브젝트에 Avatar Mask 적용

Avatar Mask의 Humanoid를 이용하여 동작 제어

Animator에서 레이어 추가 후 설정에서 가중치 최대(1)로 설정 후 Mask에 만들어 놓은 Avata Mask를 추가해주고, 다른 동작을 드레그 앤 드랍으로 옮겨 준다

즉, Avata Mask를 활용하여 모션들을 제어하고 추가 할 수 있다

Blend Tree 1D

오브젝트 생성 및 Animator Controller 적용

Brend Tree 생성

Brend Tree 설정 및 Motion 등록

단축키 등록

Blend Tree 제어 스크립트 작성

대기, 방향키(걷기), 뛰기(shift + 방향키)

using System.Collections;
using System.Collections.Generic;
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"); // 위, 아래 방향키 입력
        // shift키를 안누르면 최대 0.5, shift키를 누르면 초대 1까지 값이 바뀌게 된다
        float offset = 0.5f + Input.GetAxis("Sprint") * 0.5f;
        //오른쪽 방향키를 누르면 forward가 +이지만 왼쪽 방향키를 누르면 forward가 -이기 때문에 애니메이션 파라미터를 설정할 땐 절대값으로 적용한다
        float moveParameter = Mathf.Abs(vertical * offset);

        // moveParameter 값에 따라 애니메이션 재생 (0: 대기, 0.5: 걷기, 1: 뛰기)
        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;
    }
}

Blend Tree 2D Simple Directional

Blend Tree 등록

변수 설정

모션등록

앞으로가기, 뒤로가기, 왼쪽, 오른쪽으로 가는 모션 등록

Blend Tree 제어 스크립트 작성

using System.Collections;
using System.Collections.Generic;
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;
    }
}

Blend Tree 2D Freefrom Directional

Blend Tree 제어 스크립트 작성

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController2DFreeformD : MonoBehaviour
{
    private Animator animator;
    // private float walkSpeed = 4.0f;
    // private float runSpeed = 8.0f;

    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;
    }
}

Blend Tree 2D Freefrom Cartesian

Blend Tree 제어 스크립트 작성

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController2DFreeformC : 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 값에 따라 애니메이션 재생 (0: 가운데, 1: 앞)
        animator.SetFloat("Vertical", vertical);
    }
}

Blend Tree Direct

Animator

애니메이션 등록 (기다림)

Avata Mask 생성

transform에 관련 캐릭터의 Avatar 등록 -> Import skeleton 클릭 -> Use Node Name에서 사용할 정보만 체크한다

Animator Layer 생성

설정에서
Weight : 1
Mask : Face Avatar

Blend Tree 생성

파라미터 등록

Blend Tree 제어 스크립트 작성

using System.Collections;
using System.Collections.Generic;
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)
    {
        // key를 누르면 파라미터 값 증가/감소 시작
        if (Input.GetKeyDown(key))
        {
            string coroutine = type == 0 ? "ParameterUp" : "ParameterDown";
            StartCoroutine(coroutine, parameter);
        }
        // key를 때면 파라미터 값 증가/감소 중지
        else if (Input.GetKeyUp(key))
        {
            string coroutine = type == 0 ? "ParameterUp" : "ParameterDown";
            StopCoroutine(coroutine);
        }

    }

    private IEnumerator ParameterUp(string parameter)
    {    
        // 현재 파라미터 값을 받아온다
        float percent = animator.GetFloat(parameter);

        // 파라미터 값을 증가시키는 코루틴이기 때문에 1이 될때까지 실행

        while (percent < 1)
        {
            percent += Time.deltaTime; // percent 값 증가
            animator.SetFloat(parameter, percent);

            yield return null;
        }
    }

    private IEnumerator ParameterDown(string parameter)
    {
        // 현재 파마미터 값을 받아온다
        float percent = animator.GetFloat(parameter);

        // 파라미터 값을 감소시키는 코루틴이기 때문에 0이 될때까지 실행
        while (percent > 0)
        {
            percent -= Time.deltaTime; // percent 값 감소 
            animator.SetFloat(parameter, percent);

            yield return null;
        }
    }
}
profile
즐겁게 살자

0개의 댓글