[3D Unity] 터치처리 사용과 기어변속기 스크립트
먼저 터치처리를 세가지 경우로 나눴습니다. if (Input.touchCount > 0 && Input.GetTouch...
blog.naver.com
먼저 터치처리를 세가지 경우로 나눴습니다.
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved)
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended)
Input.GetTouch (0).phase == TouchPhase.Began : 처음 터치가 됐을 경우
Input.GetTouch (0).phase == TouchPhase.Moved : 터치 후 움직이고 있을 경우
Input.GetTouch (0).phase == TouchPhase.Ended : 터치 후 뗄 경우
여기 스크립트에서는 Began과 Ended만 사용을 하고있기에 Moved부분은 주석처리를 했습니다.
변속기가 동작 하는 경우는 터치 후 뗄 경우. 즉, Input.GetTouch (0).phase == TouchPhase.Ended 경우 일 때 발생하게 됩니다.
기어변속을 할 때 사람들이 변속기를 손으로 위아래로 조절하는 것을 터치&드래그로 표현 하기 위해 처음 터치를 한 곳(Began이 실행 된 부분)의 위치 좌표를 _deltaPosX_Start와 _deltaPosY_Start에 받고, 터치후 뗄 경우(.Ended가 실행 된 부분)의 위치 좌표를_deltaPosX_End와 _deltaPosY_End에 넣습니다.
그 후 _deltaPosY_Start와 _deltaPosY_End의 좌표를 비교하여 _deltaPosY_Start 좌표가 _deltaPosY_End 보다 값이 크면 아래로 드래그를 한 것이고, 반대의 경우에는 위로 드래그를 한 것을 알 수 있게 하도록 구현했습니다.
아래로 드래그를 하게되면 레버를 아래로, 위로 드래그를 하게되면 레버를 위로 올리는 작업을 해줍니다. 또한 P,R,N,D 중에 어떤 상태인지 구별해주기 위해 각각의 게임오브젝트를 받아와서 활성화 및 비활성화 시키는 작업을 해줍니다.
------------------------------------------------------
using UnityEngine;
using System.Collections;
public class GearLever : MonoBehaviour {
private float _halfHeight;
private float _halfWidth;
//float _deltaPosX;
//float _deltaPosY;
float _deltaPosX_End;
float _deltaPosY_End;
float _deltaPosX_Start;
float _deltaPosY_Start;
public GameObject _p;
public GameObject _r;
public GameObject _n;
public GameObject _d;
// Use this for initialization
void Start () {
_halfHeight = Screen.height * 0.5f; // 현재 게임 화면의 세로 해상도를 반환함. 세로해상도의 1/2값이 필요하므로 0.5를 곱함.
_halfWidth = Screen.width * 0.5f;
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) // 터치한 직후
{
_deltaPosX_Start = Input.GetTouch(0).position.x - _halfWidth;
_deltaPosY_Start = Input.GetTouch(0).position.y - _halfHeight;
}/*
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) // 터치후 움직이고 있을 때
{
_deltaPosX = Input.GetTouch(0).position.x - _halfWidth;
_deltaPosY = Input.GetTouch(0).position.y - _halfHeight;
}*/
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended) // 터치후 움직이다 뗐을 경우
{
_deltaPosX_End = Input.GetTouch(0).position.x - _halfWidth;
_deltaPosY_End = Input.GetTouch(0).position.y - _halfHeight;
if(_deltaPosY_Start < -30 && _deltaPosY_Start > -230) // 처음 터치 부분의 범위
{
if(_deltaPosX_Start > 150 && _deltaPosX_Start < 170) // 처음 터치 부분의 범위가 이럴 경우 (이미지를 클릭했냐 안했냐)
{
if(_deltaPosY_Start > _deltaPosY_End) //내리는 것
{
transform.Translate(0, -0.15f,0);
if(transform.localPosition.y < -280 )
transform.Translate(0, 0.15f,0);
}
if(_deltaPosY_Start < _deltaPosY_End) //올리는 것
{
transform.Translate(0, 0.15f,0);
if(transform.localPosition.y > -99 )
transform.Translate(0, -0.15f,0);
}
}
}
//초기화 시켜주는 부분
_p.SetActive(false);
_r.SetActive(false);
_n.SetActive(false);
_d.SetActive(false);
if(transform.localPosition.y < -90 && transform.localPosition.y > -110) _p.SetActive(true);
if(transform.localPosition.y < -150 && transform.localPosition.y > -170) _r.SetActive(true);
if(transform.localPosition.y < -210 && transform.localPosition.y > -230) _n.SetActive(true);
if(transform.localPosition.y < -270 && transform.localPosition.y > -290) _d.SetActive(true);
}
}
}
--------------------------------------------------------
여기까지가 현재 만들고있는 게임의 기어변속기 스크립트입니다.
C#스크립트로 만들었습니다.
'Unity' 카테고리의 다른 글
유니티 물체기준 회전 (0) | 2020.10.10 |
---|---|
마우스 버튼 읽기 (0) | 2020.10.10 |
Unity ARsubsystems Trackabletype (0) | 2020.09.01 |
MonoBehaviour messages(기본함수) (0) | 2020.08.25 |
object.FindObjectOfType unity C# script (0) | 2020.08.25 |