아미(아름다운미소)

유니티 이미지 플립 (오브젝트 이미지 뒤집기) 본문

랭귀지/Unity

유니티 이미지 플립 (오브젝트 이미지 뒤집기)

유키공 2018. 4. 7. 09:30
유니티에서 이미지를 뒤집어야 할 경우가 종종 있는데요. 플레이어가 왼쪽에서 오른쪽으로 또는 오른쪽에서 왼쪽으로 이동시 이미지는 오른쪽이나 왼쪽 한장만 있을 시 오브젝트의 이미지를 플립 하시면 되는데요. 오브젝트의 이미지를 플립하는 두가지 방법 입니다. 

 1. 오브젝트의 스케일을 조정해서 뒤집는 방법
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using UnityEngine;
using System.Collections;
  
public class CsFly : MonoBehaviour {
    public int speed = 5;
    // Use this for initialization
    void Start () {
      
    }
      
    // Update is called once per frame
    void Update () {
        float key = Input.GetAxis("Horizontal");
        float amtMove = speed * Time.smoothDeltaTime;
  
        if(key > 0)
        {
            //왼쪽 이동
            Vector3 scale = transform.localScale;
            scale.x = -Mathf.Abs(scale.x);
            transform.localScale = scale;
  
            transform.Translate(Vector3.right * amtMove * key);
        }
        else if( key < 0)
        {
            //오른쪽이동
            Vector3 scale = transform.localScale;
            scale.x = Mathf.Abs(scale.x);
            transform.localScale = scale;
              
            transform.Translate(Vector3.right * amtMove * key);   
        }
  
    }
}

2. 오브젝트의 메트리얼의 타일링을 이용해서 뒤집는 방법
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using UnityEngine;
using System.Collections;
  
public class CsFly : MonoBehaviour {
  
    public int speed = 5;
    // Use this for initialization
    void Start () {
      
    }
      
    // Update is called once per frame
    void Update () {
        float key = Input.GetAxis("Horizontal");
        float amtMove = speed * Time.smoothDeltaTime;
  
        if(key > 0)
        {
            //왼쪽 이동
            Vector2 tiling = transform.renderer.material.mainTextureScale;
            tiling.x = Mathf.Abs(tiling.x);
            transform.renderer.material.mainTextureScale = tiling;
            transform.Translate(Vector3.right * amtMove * key);
        }
        else if( key < 0)
        {
            //왼쪽 이동
            Vector2 tiling = transform.renderer.material.mainTextureScale;
            tiling.x = -Mathf.Abs(tiling.x);
            transform.renderer.material.mainTextureScale = tiling;        
            transform.Translate(Vector3.right * amtMove * key);   
        }
  
    }
}



Comments