Search Unity

Vertical Plane Detection help.

Discussion in 'AR/VR (XR) Discussion' started by marcosis, Jul 2, 2019.

  1. marcosis

    marcosis

    Joined:
    Nov 5, 2018
    Posts:
    22
    Hey!

    I have this script that uses a placement tracker for arfoundation markerless AR.

    When the screen is clicked it places a gameobject on the plane it has detected.

    I now need it to work for vertical plane detection although if someone has another way, id be happy to use that too.

    Im new at all this, so looking at it, its not obvious for me what to change. The only thing i tried was changing the var cameraBearing = new Vector3(cameraForward.x, cameraForward.y, 0).normalized;

    and sticking a zero into one of the other axis, but to no avail.

    Any help work be much appreciated.

    Cheers

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.XR.ARFoundation;
    using UnityEngine.Experimental.XR;
    using System;

    public class ARTapToPlaceObject : MonoBehaviour {

    public GameObject placementIndicator;
    public GameObject objectToPlace;
    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()
    {
    if (placementPoseIsValid)
    {
    placementIndicator.SetActive(true);
    placementIndicator.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
    }
    else
    {
    placementIndicator.SetActive(false);
    }
    }

    private void UpdatePlacementPose(){

    var screenCentre = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
    var hits = new List<ARRaycastHit>();
    arOrigin.Raycast(screenCentre, hits, TrackableType.Planes);

    placementPoseIsValid = hits.Count > 0;
    if (placementPoseIsValid)
    {
    placementPose = hits[0].pose;

    var cameraForward = Camera.current.transform.forward;
    var cameraBearing = new Vector3(cameraForward.x, cameraForward.y, 0).normalized;
    placementPose.rotation = Quaternion.LookRotation(cameraBearing);

    }

    }
    }