0

使用 8th Wall XR 是否可以在表面上单击并使其成为活动表面并将游戏对象放置在单击位置上?有点像 ARKit,它只在点击后增加游戏对象。

4

1 回答 1

1

像这样的东西应该可以解决问题。您需要一个附加了 XRSurfaceController 的 GameObject(即一个平面),并将其放在一个名为“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);
  }
}
于 2018-01-18T21:39:05.800 回答