简体   繁体   English

为什么我的输入触摸代码在 AR Foundation 上不起作用?

[英]Why my Input touch code doesn't work on AR Foundation?

Trying to build an AR app where I can have a number of input touch like drag, rotate, scale, double tap to event, hold on object to event etc. Everything works fine in a test scene I have built [not AR].尝试构建一个 AR 应用程序,我可以在其中进行许多输入触摸,例如拖动、旋转、缩放、双击事件、按住 object 到事件等。在我构建的测试场景中一切正常[不是 AR]。 Once included the code in my AR placedOnPlane prefab [template scene - place on plane], once I touch the object, it disappears, and I cannot figure out what I am doing wrong!一旦将代码包含在我的 AR placedOnPlane 预制件 [模板场景 - 放置在平面上],一旦我触摸 object,它就会消失,我无法弄清楚我做错了什么!

Finally, I took advantage of LeanTouch and everything works fine (why? because it's a badass asset), but I usually hate using assets when I have my code that is working equally good and I spent days on it.最后,我利用了 LeanTouch 并且一切正常(为什么?因为它是一个糟糕的资产),但我通常讨厌使用资产,因为我的代码工作得同样好并且我花了几天时间在它上面。 Some help please.请帮忙。

I tried commenting out the built in drag function in the PlacedOnPlane code that comes with the scene of ARfoundation but it didn't work.我尝试在 ARfoundation 场景附带的 PlacedOnPlane 代码中注释掉内置拖动 function 但它不起作用。

using UnityEngine;
using System.Collections;
using UnityEngine.iOS;

public class InputTouchUnity : MonoBehaviour
{
    private Vector3 position;
    private float width;
    private float height;
    public float speedDrag= 0.1f;
    float initialFingersDistance;
    float speedTwist = -4000f;
    private float baseAngle = 0.0f;
    Vector3 initialScale;

 // scale clamp

     //public float scalingSpeed = 0.03f;
     public Vector3 min = new Vector3(292f, 292f, 292f);
     public Vector3 max = new Vector3(800f, 800f, 800f);

    // int tapCount;
    // float doubleTapTimer;

    void Awake()
    {
        width = (float)Screen.width / 2.0f;
        height = (float)Screen.height / 2.0f;

        //Position used for the cube.
        position = this.transform.position;
    }

    void OnGUI() // TO OBSERVE MOTION
    {
        // Compute a fontSize based on the size of the screen width.
        GUI.skin.label.fontSize = (int)(Screen.width / 25.0f);

        GUI.Label(new Rect(20, 20, width, height * 0.25f),
            "x = " + position.x.ToString("f2") +
            ", y = " + position.z.ToString("f2"));
    }
    void Update()
    {



        // Handle screen touches.
        if (Input.touchCount > 0)
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))

            initialScale = transform.localScale;

            {

                {

                    {
                         //DRAG - got rid of it because conflicting the AR drag
                            Touch touch = Input.GetTouch(0);
                          Move the cube if the screen has the finger moving.
                          if (Input.touchCount == 2)
                         {

                           if (touch.phase == TouchPhase.Moved)
                          {
                              Vector2 pos = touch.position;
                              pos.x = (pos.x - width) / width;
                              pos.y = (pos.y - height) / height;
                              position = new Vector3(transform.position.x + pos.x * speedDrag, 0, transform.position.y + pos.y * speedDrag);
                              // Position the cube.
                             transform.position = position;
                          }
                        }

                        //SCALE
                         if (Input.touchCount == 2)
                         {
                             Touch touch1 = Input.GetTouch(0);

                             if (touch1.phase == TouchPhase.Began)
                             {
                                 initialFingersDistance = Vector2.Distance(Input.touches[0].position , Input.touches[1].position);
                                 initialScale = transform.localScale;
                             }
                             else
                             {
                                 var currentFingersDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
                                var scaleFactor = (currentFingersDistance  / initialFingersDistance );
                                 transform.localScale = initialScale * scaleFactor;
                                 Debug.Log(transform.localScale);

                                 GameObject[] models = GameObject.FindGameObjectsWithTag ("ARobject");
                                   newScale.x = Mathf.Clamp(model.localScale.x - scaleFactor, min.x, max.x);
                                newScale.y = Mathf.Clamp(model.localScale.y - scaleFactor, min.y, max.y);
                                  newScale.z = Mathf.Clamp(model.localScale.z - scaleFactor, min.z, max.z);
                                   model.localScale = newScale;

                           }
                         }
                        //TWIST

                        if (Input.touchCount == 2)
                        {
                            Touch touch2 = Input.GetTouch(0);

                            if (touch2.phase == TouchPhase.Began)
                            {
                                Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
                                pos = Input.mousePosition - pos;
                                baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Deg2Rad;
                                baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
                            }
                            if (touch2.phase == TouchPhase.Moved)
                            {
                                Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
                                pos = Input.mousePosition - pos;
                                float ang = Mathf.Atan2(pos.y, pos.x) * Mathf.Deg2Rad - baseAngle;
                                transform.rotation = Quaternion.AngleAxis(ang * speedTwist, Vector3.up);
                            }
                        }

                    }
                }
            }

        }
    }
}
//}

This is because you are using Physics.Raycast which does't work on AR trackables(Planes) because they don't have any specific geometry associated with them.这是因为您使用的 Physics.Raycast 不适用于 AR 可跟踪(平面),因为它们没有任何特定的几何图形与之关联。 So to interact with trackable data unity has provided separate Raycast method that is available in ARRaycastManager in ARFoundation.因此,为了与可跟踪数据交互,unity 提供了单独的 Raycast 方法,该方法在 ARFoundation 中的 ARRaycastManager 中可用。 In previous version of ARFoundation it was available in ARSessionOrigin.在以前版本的 ARFoundation 中,它在 ARSessionOrigin 中可用。 So check which version you are using of Ar Foundation.因此,请检查您使用的 Ar Foundation 版本。 You can use it like this你可以像这样使用它

enter code here在此处输入代码

` `

[SerializeField] ARRaycast​Manager raycastManager;
    void Update()
        {
             if (Input.touchCount == 0)
                return;
           Touch touch = Input.GetTouch(0);

           if (raycastManager.Raycast(touch.position,s_Hits,TrackableType.PlaneWithinPolygon))
            {
                // Raycast hits are sorted by distance, so the first one
                // will be the closest hit.
                var hitPose = s_Hits[0].pose;
                if (spawnedObject == null)
                {
                    spawnedObject = Instantiate(cube, hitPose.position, hitPose.rotation);
                }
                else
                {
                    spawnedObject.transform.position = hitPose.position;
                }
            }
        }

` `

You can also refer to SimpleAR scene from Ar Foundation sample scenes available here: https://github.com/Unity-Technologies/arfoundation-samples您还可以在此处参考 Ar Foundation 示例场景中的 SimpleAR 场景: https://github.com/Unity-Technologies/arfoundation-samples

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM