본문 바로가기
유니티강좌

유니티 오브젝트(캐릭터) 이동시 이미지 방향 좌우반전 구현 - 유니티 2D 기초강좌. Unity & C# Script(Object Flip Effect - Left / Right)

by Ncube 2020. 12. 2.

 

 

유니티에서 오브젝트(캐릭터)의 이동시 가는 쪽으로 이미지의 방향을 바꾸는 방법 구현.

 

#Flip #좌우반전 #유니티

 

 

[FlipPlayer.cs]

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

public class FlipPlayer : MonoBehaviour
{
    [SerializeField] [Range(1f, 10f)] float moveSpeed = 3f;

    void Update()
    {
        Vector3 flipMove = Vector3.zero;

        if(Input.GetAxisRaw("Horizontal") < 0)
        {
            flipMove = Vector3.left;
            transform.localScale = new Vector3(-1f, 1f, 1f);
        }
        else if(Input.GetAxisRaw("Horizontal") > 0)
        {
            flipMove = Vector3.right;
            transform.localScale = new Vector3(1f, 1f, 1f);
        }

        transform.position += flipMove * moveSpeed * Time.deltaTime;
    }
}

 

댓글