Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

ARFoundation need help!

Discussion in 'AR' started by cs152051, Feb 19, 2019.

  1. cs152051

    cs152051

    Joined:
    Feb 17, 2019
    Posts:
    4
    I just made simple application of placing object on plane but this error coming dont know why can any one help? i have attached the image.


    here's my code :

    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 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()
    {
    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.Raycast(screenCenter, 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, 0, cameraForward.z).normalized;
    placementPose.rotation = Quaternion.LookRotation(cameraBearing);
    }
    }
    }
     

    Attached Files:

    • 1.png
      1.png
      File size:
      247.8 KB
      Views:
      537
  2. XRDigitech05

    XRDigitech05

    Joined:
    Feb 14, 2019
    Posts:
    4
    hi
    i think u forgot to drag prefab object for instantiate in unity hierarchy.
     
  3. hammadzahidfinz

    hammadzahidfinz

    Joined:
    Jun 25, 2018
    Posts:
    6
    have you done?
     
  4. Dinesh-IMPROWISESTUDIOS

    Dinesh-IMPROWISESTUDIOS

    Joined:
    Oct 31, 2017
    Posts:
    1
    try to add AR Raycast Manager to ARSessionOrigin & AR Camera Manager to MainCamera in ARFoundation 2.0
     
unityunity