Skip to content

Instantly share code, notes, and snippets.

@kimsama
Last active December 18, 2015 04:39
Show Gist options
  • Select an option

  • Save kimsama/5726979 to your computer and use it in GitHub Desktop.

Select an option

Save kimsama/5726979 to your computer and use it in GitHub Desktop.
카메라가 회전했는데도 fingerPos 값은 이전 벡터의 방향 그대로여서 나타나는 문제 같습니다. OnMyFingerDragMove 함수에서 tempFingerPos 벡터를 카메라가 회전한 만큼 회전 시킨 다음 그 값을 적용해 보기 바랍니다.
using UnityEngine;
using System.Collections;
public class PickupAndMove : MonoBehaviour
{
public GameObject targetObject;
public GameObject noTargetObject; // 선택하면 안되는 오브젝트(테이블).
private int dragFingerIndex = -1;
Rigidbody tempRigbody;
void OnEnable()
{
FingerGestures.OnFingerTap += OnMyFingerTap;
FingerGestures.OnFingerDragBegin += OnMyFingerDragBegin;
FingerGestures.OnFingerDragMove += OnMyFingerDragMove;
FingerGestures.OnFingerDragEnd += OnMyFingerDragEnd;
}
void OnDisable()
{
FingerGestures.OnFingerTap -= OnMyFingerTap;
FingerGestures.OnFingerDragBegin -= OnMyFingerDragBegin;
FingerGestures.OnFingerDragMove -= OnMyFingerDragMove;
FingerGestures.OnFingerDragEnd -= OnMyFingerDragEnd;
}
void OnMyFingerTap ( int fingerIndex, Vector2 fingerPos, int tapCount ) // 클릭 오브젝트 확인용.
{
GameObject selection = PickObject(fingerPos);
if(selection != null)
{
Debug.Log ("Your Select Object = " + selection.name);
}
Debug.Log ("Finger Position = " + GetTestPos(fingerPos));
}
void OnMyFingerDragBegin ( int fingerIndex, Vector2 fingerPos, Vector2 startPos )
{
GameObject selection = PickObject (fingerPos);
if(selection != null && selection != noTargetObject)
{
targetObject = selection;
dragFingerIndex = fingerIndex;
tempRigbody = targetObject.GetComponent<Rigidbody>();
tempRigbody.useGravity = false;
tempRigbody.constraints = RigidbodyConstraints.FreezeAll;
Debug.Log ("PickUP Object : " + targetObject.name);
}
}
void OnMyFingerDragMove ( int fingerIndex, Vector2 fingerPos, Vector2 delta )
{
if(dragFingerIndex == fingerIndex)
{
Vector3 tempFingerPos = GetWorldPos(fingerPos);
tempFingerPos.z = targetObject.transform.position.z; // z를 오브젝트 좌표로 고정한다.
if(tempFingerPos.y < 0.151f) tempFingerPos.y = 0.151f; // 테이블 밑으로 못내려가게 한다.
// 여기에서 tempFingerPos 벡터를 카메라가 회전한만큼 회전시킨 다음에
// targetObject.transform.position 값을 변경해 보기 바랍니다.
// 예) 90도 회전한 경우
// tempFingerPos = Quaternion.Euler(0f, 90f, 0f) * tempFingerPos;
targetObject.transform.position = tempFingerPos;
Debug.Log ("Moving Object : " + targetObject.name);
}
}
void OnMyFingerDragEnd ( int fingerIndex, Vector2 fingerPos )
{
if(dragFingerIndex == fingerIndex)
{
dragFingerIndex = -1;
tempRigbody.useGravity = true;
tempRigbody.constraints = RigidbodyConstraints.None;
Debug.Log ("Release Object : " + targetObject.name);
}
}
public static GameObject PickObject( Vector2 screenPos )
{
Ray ray = Camera.main.ScreenPointToRay( screenPos );
RaycastHit hit;
if( Physics.Raycast( ray, out hit ) )
return hit.collider.gameObject;
return null;
}
public static Vector3 GetWorldPos( Vector2 screenPos )
{
Ray ray = Camera.main.ScreenPointToRay( screenPos );
// we solve for intersection with z = 0 plane
float t = -ray.origin.z / ray.direction.z;
return ray.GetPoint( t );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment