[英]AR Vuforia move object by touch only in x z (Unity3D)
我正在開發增強現實程序,我需要通過觸摸x和z坐標來移動對象。 我在vuforia網站上找到了用於移動對象的代碼。 我測試了這段代碼,但是有一些問題,例如對象在Y中移動,所以它飛到了空中! 但是我只想在xz上(在地面場景上)移動我的對象,我試圖更改代碼以自己完成任務,但沒有任何結果。 如果知道的話,請幫助我。
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;
}
}
}
}
對於對vuforia操縱感興趣的任何人,您都可以使用Lean Touch庫進行平移,旋轉,縮放等操作(它將被棄用,它也同樣有效)。
不幸的是,Lean Touch還沒有一個腳本可以在zOx平面中翻譯。 為此,我根據aldonaletto的答案制作了這個腳本。 對於多個對象,您可以使用Unity的Touch API輕松添加某種選擇
using UnityEngine;
public class MyDragBehaviour : MonoBehaviour
{
void Update()
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
// create ray from the camera and passing through the touch position:
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
// create a logical plane at this object's position
// and perpendicular to world Y:
Plane plane = new Plane(Vector3.up, transform.position);
float distance = 0; // this will return the distance from the camera
if (plane.Raycast(ray, out distance))
{ // if plane hit...
Vector3 pos = ray.GetPoint(distance); // get the point
transform.position = pos; // pos has the position in the plane you've touched
}
}
}
}
在我的情況下,我將它們用於地平面檢測,但與基於目標的增強效果一樣好
我昨天解決了這個問題。 您需要選擇世界中心。 將ARCamera的“世界中心模式”參數從“ FIRST_TARGET”選擇為“ SPECIFIC_TARGET”,然后選擇目標,即虛擬世界中心。 然后在“播放”模式下嘗試。 角色將在您的SPECIFIC_TARGET X和Z軸上移動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.