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

问题描述 投票:2回答:1

[尝试构建一个AR应用程序,在该应用程序中我可以进行许多输入操作,例如拖动,旋转,缩放,双击事件,按住对象事件等。在我构建的测试场景中,一切正常[不是AR]。一旦将代码包含在我的AR placedOnPlane预制模板中[模板场景-放置在平面上],一旦我触摸了该对象,它就会消失,并且我无法弄清楚自己在做什么错!

[最后,我利用了LeanTouch,一切都很好(为什么?因为这是一个坏蛋资产),但是当我的代码运行得很好并且花了很多时间时,我通常讨厌使用资产!请提供一些帮助。

[我尝试注释掉ARfoundation场景随附的PlacedOnPlane代码中的内置拖动功能,但是它不起作用。

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);
                            }
                        }

                    }
                }
            }

        }
    }
}
//}
c# unity3d input touch augmented-reality
1个回答
0
投票

您正在执行一些标头和功能。本教程将为您提供更好的帮助:-

https://youtu.be/xguiSueY1Lw

© www.soinside.com 2019 - 2024. All rights reserved.