我正在使用ArFoundation构建一个将对象放置在地平面上的应用程序。我为此目的构建了一个放置指示器,但是我面临的问题是它是第一次生成,但是指示器并没有随相机一起移动。有人可以帮我吗。在下面提供我的代码。我仅使用android进行了测试。
我还创建了一个功能,可以将对象放置在水龙头上,这也不起作用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.Experimental.XR;
using System;
public class ArTapToPlace : MonoBehaviour
{
public GameObject objectToPlace;
public GameObject placementIndicator;
private ARSessionOrigin arOrigin;
private Pose placementPose;
private bool placementPoseIsValid = false;
void Start()
{
arOrigin = FindObjectOfType<ARSessionOrigin>();
}
void Update()
{
UpdatePlacementPose();
UpdatePlacementIndicator();
if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
PlaceObject();
}
}
private void PlaceObject()
{
Instantiate(objectToPlace,placementPose.position,placementPose.rotation);
}
private void UpdatePlacementIndicator()
{
print(placementIndicator);
if (placementPoseIsValid)
{
placementIndicator.Setactive(true);
placementIndicator.transform.SetPositionAndRotation(placementPose.position,placementPose.rotation);
}
else
{
placementIndicator.Setactive(false);
}
}
private void UpdatePlacementPose()
{
var screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f,0.5f));
var hits = new List<ARRaycastHit>();
arOrigin.getcomponent<ARRaycastManager>().Raycast(screenCenter,hits,UnityEngine.XR.ARSubsystems.TrackableType.Planes);
print(hits.Count);
print(hits);
placementPoseIsValid = hits.Count > 0;
if (placementPoseIsValid)
{
placementPose = hits[0].pose;
var cameraForward = Camera.current.transform.forward;
var cameraBearing = new Vector3(cameraForward.x,cameraForward.z).normalized;
placementPose.rotation = Quaternion.LookRotation(cameraBearing);
}
}
}