You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.0 KiB
74 lines
2.0 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.XR.ARFoundation;
|
|
using UnityEngine.XR;
|
|
using UnityEngine.XR.ARSubsystems;
|
|
using System;
|
|
|
|
public class arTap : MonoBehaviour
|
|
{
|
|
public GameObject objectToPlace;
|
|
public GameObject placementIndicator;
|
|
private Camera myCamera;
|
|
private ARSessionOrigin arOrigin;
|
|
private ARRaycastManager raycastManager;
|
|
private Pose placementPose;
|
|
private bool placementPoseIsValid = false;
|
|
|
|
void Start()
|
|
{
|
|
arOrigin = FindObjectOfType<ARSessionOrigin>();
|
|
raycastManager = FindObjectOfType<ARRaycastManager>();
|
|
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
UpdatePlacementPose();
|
|
UpdatePlacementIndicator();
|
|
Debug.Log(placementPoseIsValid);
|
|
|
|
if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
|
|
{
|
|
PlaceObject();
|
|
}
|
|
}
|
|
|
|
private void PlaceObject()
|
|
{
|
|
Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
|
|
}
|
|
|
|
private void UpdatePlacementIndicator()
|
|
{
|
|
if (placementPoseIsValid)
|
|
{
|
|
placementIndicator.SetActive(true);
|
|
placementIndicator.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
|
|
}
|
|
else
|
|
{
|
|
placementIndicator.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private void UpdatePlacementPose()
|
|
{
|
|
var screenCenter = Camera.main.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
|
|
var hits = new List<ARRaycastHit>();
|
|
raycastManager.Raycast(screenCenter, hits, TrackableType.Planes);
|
|
Debug.Log(hits);
|
|
|
|
placementPoseIsValid = hits.Count > 0;
|
|
if (placementPoseIsValid)
|
|
{
|
|
placementPose = hits[0].pose;
|
|
|
|
var cameraForward = Camera.main.transform.forward;
|
|
var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized;
|
|
placementPose.rotation = Quaternion.LookRotation(cameraBearing);
|
|
}
|
|
}
|
|
|
|
}
|