2014년 6월 23일 월요일

[유니티] C# 터치 드래그와 Transform 클래스...

터치 드래그에 따라 카메라 위치를 이동시켜보려고 하다가... 엄청난 삽질을 하고 말았다.

바로 Transform 클래스!!!

터치 좌표(Vector2)를 가져와서 카메라 이동을 위해, Vector3으로 변환해서 넣었는데... 
뭔가 되긴 되는데 좌표가 이상하게 이동하더라는 것이다.

범인은 이 코드였다.


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 TouchCameraMover : MonoBehaviour 
{
    public float Speed;
    public Vector2 nowPos, prePos;
    public Vector3 movePos;
    void Update()
    {
        if(Input.touchCount == 1)
        {
            Touch touch = Input.GetTouch (0);
            if(touch.phase == TouchPhase.Began)
            {
                prePos = touch.position - touch.deltaPosition;
            }
            else if(touch.phase == TouchPhase.Moved)
            {
                nowPos = touch.position - touch.deltaPosition;
                movePos = (Vector3)(prePos - nowPos) * Speed;
                camera.transform.position += movePos; // 뭐가 문제란 말이냐... ㅜ.ㅜ
                prePos = touch.position - touch.deltaPosition;
            }
            else if(touch.phase == TouchPhase.Ended)
            {
            }
        }
    }
}

position으로 값을 그냥 가져오길래... 난 더하기도 될 줄 알았네. 저렇게 하면 안되나 보다.
구글 이곳저곳 살짝 보기에, 저런식으로는 허용을 안 하는 것 같다. 
함수를 써서 값을 바꾸는게 정석인 것으로 보인다.
(동적으로 vector3을 할당해서 넣어도 되는거 같기도 한데... 정확한 내용은 나중에 알게 되면 다시 업데이트를...)

Vector3이면, 내가 좌측으로 이동시키면 x, y, z가 전부 바뀌어야 하는데... 어째 x, y만 바뀌더라... 흠...??? 왜죠??? 하다가 다음과 같이 바꾸어보았다.


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
using UnityEngine;
using System.Collections;
public class TouchCameraMover : MonoBehaviour 
{
    public float Speed;
    public Vector2 nowPos, prePos;
    public Vector3 movePos;
    void Update()
    {
        if(Input.touchCount == 1)
        {
            Touch touch = Input.GetTouch (0);
            if(touch.phase == TouchPhase.Began)
            {
                prePos = touch.position - touch.deltaPosition;
            }
            else if(touch.phase == TouchPhase.Moved)
            {
                nowPos = touch.position - touch.deltaPosition;
                movePos = (Vector3)(prePos - nowPos) * Speed;
                camera.transform.Translate(movePos); // 이럴수가 !!!!!!!
                prePos = touch.position - touch.deltaPosition;
            }
            else if(touch.phase == TouchPhase.Ended)
            {
            }
        }
    }
}

잘 된다... x, y, z 값도 전부 잘 바뀌고.
이것 때문에 원인을 찾으려고 엄청 고생했다. 덕분에 디버그 모드로 로그도 찍어보고 ^^;;;
대충 때려 박으면 안되는구나 ㅋㅋㅋ
C# 문법 공부의 필요성이 점점 대두되고 있다. 

댓글 없음:

댓글 쓰기