【问题标题】:AR Vuforia move object by touch only in x z (Unity3D)AR Vuforia 仅在 x z 中通过触摸移动对象(Unity3D)
【发布时间】: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


    【解决方案1】:

    对于任何对 vuforia 操作感兴趣的人,您可以使用 Lean Touch library 进行平移、旋转、缩放等等(它会说它已被弃用,它也可以正常工作)。

    不幸的是,Lean Touch 没有在 zOx 平面中翻译的脚本。为此,我根据aldonaletto's answer 制作了这个脚本。对于多个对象,您可以使用 Unity's 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
                    }
                }
            }
        }
    

    在我的例子中,我将它们用于地平面检测,但它也适用于基于目标的增强

    【讨论】:

      【解决方案2】:

      我昨天解决了这个问题。您需要选择世界中心。从“FIRST_TARGET”到“SPECIFIC_TARGET”选择您的 ARCamera“世界中心模式”参数并选择您的目标,即您的虚拟世界中心。然后在播放模式下尝试。角色将在您的 SPECIFIC_TARGET X & Z 轴上移动。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-10
        • 2018-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多