🎮 Unity 개발/Unity

[유니티] 애니메이션 제어 - Animator / Animator Controller

gameuiux 2024. 12. 27. 03:17
728x90
반응형

Animator 파라미터

Float: 실수형 값
Int: 정수형 값
Bool: 참/거짓 값
Trigger: 단발성 이벤트


예시
isRunning (Bool) → 달리기 상태
Speed (Float) → 이동 속도
Jump (Trigger) → 점프 이벤트

 

 

전이 조건(Transition) 설정

Make Transition


조건 설정
Transition을 선택한 뒤 Inspector 창에서 Conditions 섹션 추가

앞서 정의한 파라미터를 조건으로 설정

 

예시 조건
Speed > 0.1 → Idle에서 Run으로 전환
isRunning == true → Walk에서 Run으로 전환

 

 

기능 정의 (스크립트로 제어)

스크립트에서 애니메이터를 불러오기

Animator 컴포넌트 연결
스크립트를 컴포넌트에 추가한 후 코드를 작성한다.

// class 아래 변수 선언
private Animator animator;



// Start 구문 아래 GetComponent 추가
void Start()
    {
        animator = GetComponent<Animator>();
    }


// Update 구문 아래 제어 조건과 기능을 작성
// Input 키를 통해 Bool값과 오브젝트의 Scale(방향)과 Position을 제어
    void Update()
{
    
    if (Input.GetKey(KeyCode.LeftArrow)) {
        Vector3 currScale = transform.localScale;
        transform.localScale = new Vector3(-Mathf.Abs(currScale.x), currScale.y, currScale.z);
        transform.position += Vector3.left * moveSpeed * Time.deltaTime;
        animator.SetBool("isRunning",true);
    } 
    else if (Input.GetKey(KeyCode.RightArrow)) {
        Vector3 currScale = transform.localScale;
        transform.localScale = new Vector3(Mathf.Abs(currScale.x), currScale.y, currScale.z);
        transform.position += Vector3.right * moveSpeed * Time.deltaTime;
        animator.SetBool("isBoolname",true);
    }
    else{
        animator.SetBool("isBoolname",false);
    }
}

 

SetFloat(name, value) → 실수형 파라미터 값 설정
SetBool(name, value) → 참/거짓 값 설정
SetTrigger(name) → 단발성 이벤트 호출
ResetTrigger(name) → 트리거 초기화

 

 

 

기타

Blend Tree
여러 애니메이션을 속도에 따라 부드럽게 전환
Animator 창에서 Create > Blend Tree 추가


State Machine Behaviour
상태별 스크립트 동작 정의
상태를 선택한 후 Add Behaviour 클릭

 

스프라이트를 여러개 드래그해서 하이어라키에 넣으면 자동으로 시퀀스 애니메이션을 만들 수 있다.

 

Missing일 경우 F2를 눌러서 이름을 하이어라키 오브젝트명과 동일하게 변경하면 해결된다.

 

키를 코드 편집기로 수정할 수 있다.

 

작은점세개를 눌러서 sample rate를 누르고 samples 값을 바꾸면 초당 스프라이트를 더 빠르게 교체할 수 있다.

 

 

 

 

 

 

728x90
반응형