我正在研究vuforia的用户定义目标包。包正在工作gud但我有问题当我尝试拖动我的3D模型。
该代码在没有UserDefinedTarget的情况下正常工作。
代码在这里:
GameObject target = GameObject.FindGameObjectWithTag("Target");
if(Input.touchCount == 1)
{
if (theTouch.phase == TouchPhase.Began)
{
Debug.Log("Touch phase began at: " + theTouch.position);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, maxPickingDistance))
{
pickedObject = target.transform;
}
else
{
pickedObject = null;
}
}
else if (theTouch.phase == TouchPhase.Moved)
{
Debug.Log("Touch phase Moved");
if (pickedObject != null)
{
Vector3 translationInCameraRef;
Vector2 screenDelta = theTouch.deltaPosition;
float halfScreenWidth = 0.5f * Screen.width;
float halfScreenHeight = 0.5f * Screen.height;
float dx = screenDelta.x / halfScreenWidth;
float dy = screenDelta.y / halfScreenHeight;
Vector3 objectToCamera =
pickedObject.transform.position - Camera.main.transform.position;
float distance = objectToCamera.magnitude;
float fovRad = Camera.main.fieldOfView * Mathf.Deg2Rad;
float motionScale = distance * Mathf.Tan(fovRad/2);
if(Application.loadedLevelName == "LoadModel")
{
translationInCameraRef =
new Vector3(motionScale * dx, motionScale * dy, 0);
}
else{
translationInCameraRef =
new Vector3(motionScale * dx , motionScale * dy , motionScale * dy);
}
Vector3 translationInWorldRef =
Camera.main.transform.TransformDirection(translationInCameraRef);
pickedObject.position += translationInWorldRef * 5.0f;
}
}
else if (theTouch.phase == TouchPhase.Ended)
{
Debug.Log("Touch phase Ended");
pickedObject = null;
}
}
有谁对此有任何想法?请帮我解决这个问题。
感谢。