ARKit - 如何沿识别的表面移动物体?

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

如何让3D物体(汽车)沿着ARKit(Unity,C#)识别的表面移动?

因此,我可以使用UI来操纵汽车,而不是手指敲击,而汽车将使用ARKit识别的表面进行移动,并在出现任何刚体效果时与之碰撞。

是否有可能使其也与其他表面(例如墙壁)发生碰撞?

c# unity3d unity5 arkit
1个回答
0
投票

这样的东西将允许您点击您的对象并拖动以沿着表面移动它。关于碰撞等 - 确保你的物体有适当的碰撞器和刚体。 ARKit目前仅提供水平表面,因此不确定如何处理墙壁碰撞......

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragAlongSurface : MonoBehaviour {

  private bool holding;

  void Start () {
    holding = false;
  }

  void Update() {

    if (holding) {
      Move();
    }

    // One finger
    if (Input.touchCount == 1) {

      // Tap on Object
      if (Input.GetTouch(0).phase == TouchPhase.Began ) {
        Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
        RaycastHit hit;

        if (Physics.Raycast (ray, out hit, 100f)) {
          if (hit.transform == transform) {
            holding = true;
          }
        }
      }

      // Release 
      if (Input.GetTouch(0).phase == TouchPhase.Ended) {
        holding = false;
      } 
    }
  }

  void Move(){
   RaycastHit hit;
   Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
   // The GameObject this script attached should be on layer "Surface"
   if(Physics.Raycast(ray, out hit, 30.0f, LayerMask.GetMask("Surface"))) {
     transform.position = new Vector3(hit.point.x,
                                      transform.position.y,
                                      hit.point.z);
    }
  }    
}
© www.soinside.com 2019 - 2024. All rights reserved.