Search Unity

Missing objects, pls help

Discussion in 'AR' started by alexcht, Mar 4, 2019.

  1. alexcht

    alexcht

    Joined:
    Mar 1, 2019
    Posts:
    1
    Hi, everyone!
    I'm new to Unity and I have a task of creating an object that appears upon tap. I'm working along this tutorial:


    The problem is that when I just place a cube, bulid&run, the cube appears floating, just like in the tutorial. Than I create a placement indicator and follow along the tutorial with code.

    When I finish it, hit build&run, both placement indicator and the object disappear. They're visible before I attach the C# script. What am I missing? I re-wrote the code by hand to make sure everything is where is should be. Tried everything I could think of and still the same result - empty device viewport.
    Pls help.

    The code is here:

    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.  
    8. public class ARTapToPlaceObject : MonoBehaviour
    9. {
    10.     public GameObject objectToPlace;
    11.     public GameObject placementIndicator;
    12.  
    13.     private ARSessionOrigin arOrigin;
    14.     private Pose placementPose;
    15.     private bool placementPoseIsValid = false;
    16.  
    17.     void Start()
    18.     {
    19.         arOrigin = FindObjectOfType<ARSessionOrigin>();
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         UpdatePlacementPose();
    25.         UpdatePlacementIndicator();
    26.  
    27.         if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    28.         {
    29.             PlaceObject();
    30.         }
    31.     }
    32.  
    33.     private void PlaceObject()
    34.     {
    35.         Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
    36.     }
    37.  
    38.     private void UpdatePlacementIndicator()
    39.     {
    40.         if (placementPoseIsValid)
    41.         {
    42.             placementIndicator.SetActive(true);
    43.             placementIndicator.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
    44.         }
    45.         else
    46.         {
    47.             placementIndicator.SetActive(false);
    48.         }
    49.     }
    50.  
    51.     private void UpdatePlacementPose()
    52.     {
    53.         var screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
    54.         var hits = new List<ARRaycastHit>();
    55.         arOrigin.Raycast(screenCenter, hits, TrackableType.Planes);
    56.  
    57.         placementPoseIsValid = hits.Count > 0;
    58.         if (placementPoseIsValid)
    59.         {
    60.             placementPose = hits[0].pose;
    61.  
    62.             var cameraForward = Camera.current.transform.forward;
    63.             var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized;
    64.             placementPose.rotation = Quaternion.LookRotation(cameraBearing);
    65.         }
    66.     }
    67. }