【发布时间】:2015-01-07 15:04:15
【问题描述】:
我正在开发一个增强现实程序,我需要在 x 和 z 坐标中通过触摸来移动对象。我在 vuforia 网站上找到了移动对象的代码。我测试了这段代码,但有一些问题,比如物体在 Y 中移动,所以它飞了!!!!!! 但我只想在 x z 中移动我的对象(在地面场景中) 我试图更改代码来做自己,但没有任何结果。 如果你得到它,请帮助我。
using UnityEngine;
using System.Collections;
public class MyDragBehaviour : MonoBehaviour
{
private float maxPickingDistance = 2000;// increase if needed, depending on your scene size
private Transform pickedObject = null;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
foreach (Touch touch in Input.touches)
{
Debug.Log("Touching at: " + touch.position);
//Gets the ray at position where the screen is touched
Ray ray = Camera.main.ScreenPointToRay(touch.position);
if (touch.phase == TouchPhase.Began)
{
Debug.Log("Touch phase began at: " + touch.position);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, maxPickingDistance))
{
pickedObject = hit.transform;
}
else
{
pickedObject = null;
}
}
else if (touch.phase == TouchPhase.Moved)
{
Debug.Log("Touch phase Moved");
if (pickedObject != null)
{
Vector2 screenDelta = touch.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);
Vector3 translationInCameraRef =
new Vector3( motionScale * dy,0, motionScale * dx);
Vector3 translationInWorldRef =
Camera.main.transform.TransformDirection(translationInCameraRef);
pickedObject.position += translationInWorldRef;
}
}
else if (touch.phase == TouchPhase.Ended)
{
Debug.Log("Touch phase Ended");
pickedObject = null;
}
}
}
}
【问题讨论】:
标签: android unity3d touch augmented-reality vuforia