본문 바로가기
개발일지/코드 기록

유니티2D 캐릭터 좌우 플립과 이동 애니메이션

by 개발자 슈니 2022. 9. 26.
728x90
반응형

우측만 바라보는 캐릭터

캐릭터가 좌측 또는 우측 한쪽만 바라보고 있을때

좌우 방향 플립을 해주는 방법이다

캐릭터 좌우 반전을 하기 위해서는 Sprite Renderer의 Flip을 이용하면 되는데,

X는 좌우, Y는 상하 반전이다

이것을 스크립트에 적용시켜 입력에 따라 플립을 시켜줄 수 있다

전에 기록해두었던 캐릭터 키보드 입력을 통한 이동 스크립트에다가

추가해주겠다

캐릭터 키보드 입력 이동 처리는 아래 링크에서 설명(주석)한다

 

유니티2D 키보드 입력에 따른 캐릭터 이동

키보드 입력에 따른 캐릭터의 이동 스크립트이다 using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerAction : MonoBehaviour { float moveSpeed = 5f; // 이동..

supersooyeon.tistory.com

 

 

 

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

public class PlayerAction : MonoBehaviour
{
    float moveSpeed = 5f;


    Rigidbody2D myRigid;

    Animator animator;

    SpriteRenderer spriteRenderer; // 플립을 위한 스프라이트 렌더러 컴포넌트 선언

    void Awake()
    {
        myRigid = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>(); // 스프라이트 렌더러 컴포넌트의 레퍼런스
    }

    void Start()
    {

    }

    private void FixedUpdate()
    {
        Move();
    }

    void Update()
    {
       
    }

    private void Move()
    {
        float moveHorizontal = Input.GetAxisRaw("Horizontal");
        float moveVertical = Input.GetAxisRaw("Vertical");

// 만약 왼쪽 키가 눌렸다면, (0보다 작은 Horizontal 키값 (-1)은 좌측을 뜻함)
        if (moveHorizontal < 0)
        {

/* 이 스크립트가 적용된 캐릭터의 기본 상태는 오른쪽을 바라보고 있기 때문에 
 유니티 에디터의 인스펙터 창에서 Sprite Renderer에서 Flip의 X를 체크해보면 좌측을 바라보게 됨
*/
            spriteRenderer.flipX = true; // X축을 체크하여라
        }

// 오른쪽 키가 눌렸다면, (0보다 큰 Horizontal 키값 (1)은 우측을 뜻함)
        else if (moveHorizontal > 0)
        {

// 기본 상태에 Flip은 아무것도 체크되어 있지 않기 때문에 false로 돌려준다
            spriteRenderer.flipX = false; // X축의 체크를 해제하여라
        }

        Vector2 moveVector = new Vector2(moveHorizontal, moveVertical);

        myRigid.velocity = moveVector * moveSpeed;

    }
}

 

 

이어서, 캐릭터에게 애니메이션을 적용시키는 코드를 추가한 스크립트이다

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

public class PlayerAction : MonoBehaviour
{
    float moveSpeed = 5f;


    Rigidbody2D myRigid;

    Animator animator;

    SpriteRenderer spriteRenderer;

    void Awake()
    {
        myRigid = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    void Start()
    {

    }

    private void FixedUpdate()
    {
        Move();
    }

    void Update()
    {
       
    }

    private void Move()
    {
        float moveHorizontal = Input.GetAxisRaw("Horizontal");
        float moveVertical = Input.GetAxisRaw("Vertical");
        if (moveHorizontal < 0)
        {
            spriteRenderer.flipX = true;

// 이 스크립트가 추가된 오브젝트의 bool형 파라미터 이름이 isWakling인 것을 실행시켜라
            animator.SetBool("isWalking", true); 
        }
        else if (moveHorizontal > 0)
        {
            spriteRenderer.flipX = false;
            animator.SetBool("isWalking", true);
        }
        else if (moveVertical < 0 || moveVertical > 0)
            animator.SetBool("isWalking", true);

// Horizontal과 Vertical의 키값이 0과 같다면,
        else if (moveHorizontal == 0 || moveVertical == 0)
            animator.SetBool("isWalking", false); // 멈춰라

        Vector2 moveVector = new Vector2(moveHorizontal, moveVertical);

        myRigid.velocity = moveVector * moveSpeed;

        
    }
}

이렇게 해서

스크립트를 통해 캐릭터의 좌우를 반전하는 것과

애니메이션을 실행하는 것을 적용시킬 수 있다.

728x90
반응형

댓글