Search Unity

ARFoundation help needed

Discussion in 'AR' started by cs152051, Apr 22, 2019.

  1. cs152051

    cs152051

    Joined:
    Feb 17, 2019
    Posts:
    4
    I am making Augmented Reality based furniture application. Currently the issue that i am facing is my object is floating after placing.
    Secondly i want to stop my plane detection after I place an object and if i have to resume again what should i do?
    Lastly I want to ask how I reset my session so all planes and objects get removed.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.ARFoundation;
    5. using UnityEngine.Experimental.XR;
    6. using System;
    7. public class ARTapToPlaceObject : MonoBehaviour
    8. {
    9.     public GameObject objectToPlace;
    10.     public GameObject placementIndicator;
    11.  
    12.     private ARSessionOrigin arOrigin;
    13.     private Pose placementPose;
    14.     private bool placementPoseIsValid = false;
    15.     private bool placementIsComplete = false;
    16.     void Start()
    17.     {
    18.         arOrigin = FindObjectOfType<ARSessionOrigin>();
    19.     }
    20.     void Update()
    21.     {
    22.         UpdatePlacementPose();
    23.         UpdatePlacementIndicator();
    24.         if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    25.         {
    26.             PlaceObject();
    27.         }
    28.     }
    29.     private void PlaceObject()
    30.     {
    31.         placementIsComplete = true;
    32.         Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
    33.  
    34.      
    35.     }
    36.     private void UpdatePlacementIndicator()
    37.     {
    38.         if (!placementIsComplete && placementPoseIsValid)
    39.         {
    40.             placementIndicator.SetActive(true);
    41.             placementIndicator.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
    42.         }
    43.         else
    44.         {
    45.             placementIndicator.SetActive(false);
    46.         }
    47.     }
    48.     private void UpdatePlacementPose()
    49.     {
    50.         var screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
    51.         var hits = new List<ARRaycastHit>();
    52.         arOrigin.Raycast(screenCenter, hits, TrackableType.Planes);
    53.         placementPoseIsValid = hits.Count > 0;
    54.         if (placementPoseIsValid)
    55.         {
    56.             placementPose = hits[0].pose;
    57.             var cameraForward = Camera.current.transform.forward;
    58.             var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized;
    59.             placementPose.rotation = Quaternion.LookRotation(cameraBearing);
    60.         }
    61.     }
    62. }