게임 개발/유니티
모바일 터치 앤 드래그 구현
FlowTree
2020. 4. 29. 22:45
반응형
Touch로 모바일에서 터치 앤 드래그를 구현하려했으나 뭔가 이상해서 다른 기능을 찾아봤다.
모바일 환경에서 Input.mousePoint와 OnMouseDrag를 이용하길래 이게 될까 싶어서 해봤는데...
잘됐다...
MouseDragObejct 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseDragObject : MonoBehaviour
{
private Vector3 mOffset;
private float mZCoord;
void OnMouseDown()
{
mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
// Store offset = gameobject world pos - mouse world pos
mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
}
private Vector3 GetMouseAsWorldPoint()
{
// Pixel coordinates of mouse (x,y)
Vector3 mousePoint = Input.mousePosition;
// z coordinate of game object on screen
mousePoint.z = mZCoord;
// Convert it to world points
return Camera.main.ScreenToWorldPoint(mousePoint);
}
void OnMouseDrag()
{
Vector3 objectPos = GetMouseAsWorldPoint() + mOffset;
objectPos.y = 0.1f;
transform.position = objectPos;
}
//마우스로 해도 모바일에선 터치로 작동되나? 왜 되지
}
반응형