【问题标题】:Moving a game object in Arcore在 Arcore 中移动游戏对象
【发布时间】:2021-01-13 16:14:42
【问题描述】:

我一直在研究 google ARCore,并陷入了如何使用来自 android 设备的输入来移动游戏对象

我创建的画布正好有 4 个按钮,它们作为 AxisTouchButton 脚本来自跨平台输入,涵盖垂直和水平。我尝试过精益触摸来缩放、平移和旋转似乎效果很好。但是当我尝试对游戏对象施加力或速度时,它第一次完美移动,然后当我再次轴按钮时,它开始了除非按下任何其他按钮,否则会在该特定方向上浮动。

以下代码用于在示例中的 HelloAR 场景中附加到 Andy 预制件的游戏对象的移动:

Vector3 offset=Vector3.zero;

offset.x = CrossPlatformInputManager.GetAxis("Horizontal");

offset.z= CrossPlatformInputManager.GetAxis("Vertical");

rb.velocity=(offset * speed ) ;

【问题讨论】:

    标签: android unity3d augmented-reality arcore


    【解决方案1】:

    我不确定为什么你的预制件会随着你提供的代码 sn-p 漂移, 完成预制件的移动后,尝试将速度重置为零。

    rb.velocity = new Vector3(0,0,0);
    

    或者可能是由于您将预制件移动到离其父锚点太远,或者可能远离 arcore 检测到的平面。

    但是我有另一种经过测试的方法,可以在 arcore 检测到的平面上使用触摸输入移动预制件,因为它允许您仅在检测到的平面上移动预制件,因此您可以在完成替换后轻松重置其锚点预制。

    我通过以下方式修改了 HelloARController.cs 脚本。

    bool move = false;   //handle move with some button calls
    void Update(){
    
        //add this in your update method to call MoveObject() method
        //handle move with some buttons
        if(move){
            MoveObject();
        }
    }
    
    void MoveObject(){
        if(Input.touchCount == 1){
            Touch touch = Input.GetTouch(0);
    
            TrackableHit hit;
            TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal;
    
            if (Frame.Raycast (touch.position.x, touch.position.y, raycastFilter, out hit)) {
                if ((hit.Trackable is DetectedPlane) && Vector3.Dot (firstPersonCamera.transform.position - hit.Pose.position, hit.Pose.rotation * Vector3.up) < 0) {
                    Debug.Log ("Hit at back of the current detected plane");
                } 
                else {// KEY CODE SNIPPET : moves the selectedObject at the location of touch on detected planes
                    selectedObject.transform.position = hit.Pose.position;
                }
            } 
            else {
                Debug.Log ("Not moving");
            }
        }
    }
    

    这里 selectedObject 是您要实例化的任何东西的预制件。 确保一次只实例化一个预制件并将其引用到 selectedObject。

    【讨论】:

      【解决方案2】:

      试用新的 ARCore 操作系统。像魅力一样工作(对于新手)。

      他们忘记在预制件上添加碰撞器,所以不要忘记在运行示例之前添加它。

      ARCore Unity SDK v1.13.0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多