简体   繁体   English

ARKit-如何放置物体而不将其附着在表面上?

[英]ARKit - how to place an object without attaching it to the surface?

Using ARKit I can place an object on the surface by tapping on the screen. 使用ARKit,我可以通过点击屏幕将对象放置在表面上。 Is it possible to place an object "in the air" instead? 是否可以将物体“悬空”放置?

For example, I want to open a portal in front of me. 例如,我想在我面前打开一个门户。 So when I tap on the screen I want the portal to appear at this location, but I don't want it to be attached to any surface. 因此,当我点击屏幕时,我希望门户网站出现在此位置,但我不希望它附着在任何表面上。

Probably I have to use coordinates of my finger tap and axes of my device, but for z-axis (distance between me and the portal) I want to use some predefined value, like 1 meter for example. 可能我必须使用手指点击的坐标和设备的轴,但是对于z轴(我和门户之间的距离),我想使用一些预定义的值,例如1米。

How can it be made in C# (for ARKit in Unity)? 如何在C#中制作(对于Unity中的ARKit)?

ScreenToWorldPoint() will do what you want. ScreenToWorldPoint()将做您想要的。 This simple example places a cube 1 meter in front of the camera. 这个简单的示例将一个立方体放在相机前面1米处。 Just replace the GameObject.CreatePrimitive() part and Instantiate() your "portal" prefab instead: 只需替换GameObject.CreatePrimitive()部分,然后将“ portal”预制件实例化Instantiate()即可:

public class PlaceObject : MonoBehaviour {

  float objDist = 1.0f;

  void Update() {
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began ) {
      Vector3 touchPos = Input.GetTouch (0).position;
      touchPos.z = objDist;
      Vector3 objPos = Camera.main.ScreenToWorldPoint (touchPos);

      GameObject myObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
      myObj.transform.position = objPos;
      myObj.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
    }
  }
}

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

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