单击活动曲面以将GameObject放置在第8个墙XR上?

时间:2018-01-18 08:49:16

标签: unity3d augmented-reality 8thwall-xr

使用第8壁XR可以点击表面并使其成为活动表面并将游戏对象放在点击位置上吗?有点像ARKit,只有在点击它后才会增加游戏对象。

1 个答案:

答案 0 :(得分:1)

这样的事情应该可以解决问题。你需要一个附有XRSurfaceController的GameObject(即一个Plane),并把它放在一个名为" Surface"的层上:

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

public class PlaceObject : MonoBehaviour {

  // Adjust this if the transform isn't at the bottom edge of the object
  public float heightAdjustment = 0.0f;

  // Prefab to instantiate.  If null, the script will instantiate a Cube
  public GameObject prefab;

  // Scale factor for instantiated GameObject
  public float objectScale = 1.0f;

  private GameObject myObj;

  void Update() {
    // Tap to place
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began ) {

      RaycastHit hit;
      Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
      // The "Surface" GameObject with an XRSurfaceController attached should be on layer "Surface"
      // If tap hits surface, place object on surface
      if(Physics.Raycast(ray, out hit, 100.0f, LayerMask.GetMask("Surface"))) {
        CreateObject(new Vector3(hit.point.x, hit.point.y + heightAdjustment, hit.point.z));
      } 
    }
  }

  void CreateObject(Vector3 v) {
    // If prefab is specified, Instantiate() it, otherwise, place a Cube
    if (prefab) {
      myObj = GameObject.Instantiate(prefab);
    } else {
      myObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
    }
    myObj.transform.position = v;
    myObj.transform.localScale = new Vector3(objectScale, objectScale, objectScale);
  }
}