Search Unity

How to initially set the placement then bring the AR model into the world?

Discussion in 'AR' started by zyonneo, Nov 27, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    I am trying to initially set the placement position and rotation first.So that the user have an idea which position and the model will be placed.Initially I am using a placement indicator which will is shown in the image below.

    This placement indicator will give an idea which is facing front.I can rotate the indicator and fix a position.Once I fix the position and when I tap on the indicator the model should come.From my code initially the placement indicator comes upon when a plane is detected.So when I touch the screen the model instantiates.I dont want that.I want to set the rotation and position of placement indicator first.After setting that I want to tap and add the model.Later add different models at different positions.
    Code (CSharp):
    1.  
    2. public class PlacementIndicator : MonoBehaviour
    3. {
    4.  
    5.     private ARRaycastManager raycastManager;
    6.     public GameObject Indicator;
    7.     public float rotspeed = 8f;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         raycastManager = FindObjectOfType<ARRaycastManager>();
    12.         Indicator.SetActive(false);
    13.      
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         List<ARRaycastHit> hits = new List<ARRaycastHit>();
    20.         raycastManager.Raycast(new Vector2(Screen.width / 2, Screen.height / 2), hits, TrackableType.Planes);
    21.  
    22.         if(hits.Count > 0)
    23.         {
    24.              transform.position = hits[0].pose.position;
    25.             //transform.rotation = hits[0].pose.rotation;
    26.  
    27.             if (!Indicator.activeInHierarchy)
    28.                 Indicator.SetActive(true);
    29.         }
    30.  
    31.         if(Input.touchCount>0 && Input.GetTouch(0).phase==TouchPhase.Moved)
    32.         {
    33.             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
    34.               transform.Rotate(Vector3.up, - touchDeltaPosition.x * rotspeed * Time.deltaTime, Space.World);
    35.               //transform.Rotate(Vector3.right, touchDeltaPosition.y * rotspeed * Time.deltaTime, Space.World);
    36.  
    37.         }
    38.  
    39.     }
    40. }
    41.  
    Object spawner code below.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ObjectSpawner : MonoBehaviour
    7. {
    8.  
    9.  
    10.     public GameObject objectToSpawn;
    11.     private PlacementIndicator placementIndicator;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         placementIndicator = FindObjectOfType<PlacementIndicator>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if(Input.touchCount>0 && Input.touches[0].phase==TouchPhase.Began)
    22.         {
    23.             GameObject gameObject = Instantiate(objectToSpawn, placementIndicator.transform.position, placementIndicator.transform.rotation);
    24.         }
    25.     }
    26. }
    27.