Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

ARfoundation - Only want to spawn object once.

Discussion in 'AR' started by Gidz, Oct 3, 2019.

  1. Gidz

    Gidz

    Joined:
    May 26, 2019
    Posts:
    4
    I'd like my object to only spawn once on the first tap of the screen. I'm creating a kids game and they tend to tap on the screen accidentally and as they look around but then they spawn the object again and its over the first one. Furthermore I have a placement indicator that needs to disappear as well after the first spawn of my object.

    I've attached my code.
     

    Attached Files:

  2. linojon

    linojon

    Joined:
    Aug 25, 2014
    Posts:
    118
    private GameObject spawnedObject;

    private void PlaceObject()
    {
    spawnedObject = Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
    }

    void Update()
    {
    ...

    if (spawnedObject==null && placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
    PlaceObject();
    }
    }
     
  3. Gidz

    Gidz

    Joined:
    May 26, 2019
    Posts:
    4
    Hi linojon, Thanks for the reply. I'm pretty new to coding and I'm not sure if I should add the code or to replace it with what I had. I tried adding it but it doesn't seem to work. Could you perhaps clarify this for me?
     
  4. Gidz

    Gidz

    Joined:
    May 26, 2019
    Posts:
    4
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.XR.ARFoundation;
    using UnityEngine.XR.ARSubsystems;
    using System;



    public class ARTapToPlaceObject : MonoBehaviour
    {
    public GameObject objectToPlace;
    public GameObject IndicatorHolder;



    private ARRaycastManager arOrigin;
    private Pose placementPose;
    private ARPlaneManager planeManager;



    private bool placementPoseIsValid = false;
    private GameObject spawnedObject;




    void Start()
    {


    IndicatorHolder = GameObject.Find("IndicatorHolder");
    arOrigin = FindObjectOfType<ARRaycastManager>();

    }

    // Update is called once per frame
    void Update()
    {
    if (spawnedObject == null && placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
    PlaceObject();
    }

    if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
    PlaceObject();
    }

    UpdateIndicatorHolder();
    UpdatePlacementPose();




    }

    private void PlaceObject()
    {
    spawnedObject = Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
    }

    private void UpdateIndicatorHolder()
    {
    if (placementPoseIsValid)
    {
    IndicatorHolder.SetActive(true);
    IndicatorHolder.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
    }
    else
    {
    IndicatorHolder.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);
    }


    }
    }
     
  5. Gidz

    Gidz

    Joined:
    May 26, 2019
    Posts:
    4
    Hi LinoJon, Thanks so much I got it working.
     
  6. midhapulkit28

    midhapulkit28

    Joined:
    Jan 18, 2019
    Posts:
    9
    Thanks Lino for this