Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question how to script a Unity3D Tap to place object at correct Geolocation using UI button to change Double

Discussion in 'AR' started by dabdaddydarko, Oct 13, 2020.

  1. dabdaddydarko

    dabdaddydarko

    Joined:
    Jul 17, 2018
    Posts:
    14
    I am currently working in unity, I am trying to at run time place an AR object in front of the user based on a button. I have two scripts called create target location and another script called locativetarget. I have createtargetlocation placed on the User camera object.I have 3 buttons on the UI canvas. I have locativetarget placed on the prefab i want placed in front of the user on click.
    How do i edit the double of locativetarget from the create target script attached to the camera object at runtime to be able to create a tap to place without ground plane.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Collections;
    7.  
    8. using UnityEngine;
    9. using UnityEngine.UI;
    10.  
    11.  
    12. public class CreateTargetLocation : MonoBehaviour
    13. {
    14.     [Header("Google Maps Coordinates")]
    15.     public double latitude = LocativeGPS.Instance.latitude;
    16.     public double longitude = LocativeGPS.Instance.longitude;
    17.     public double altitude = LocativeGPS.Instance.altitude;
    18.  
    19.    
    20.     public AudioSource kickSource;
    21.     public Camera nonVRCamera;
    22.     public GameObject prefab;
    23.     //Make sure to attach these Buttons in the Inspector
    24.     public Button m_YourFirstButton, m_YourSecondButton, m_YourThirdButton;
    25.  
    26.     void Start()
    27.     {
    28.        
    29.         // Start service before querying location
    30.         Input.location.Start();
    31.         //Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button
    32.         m_YourFirstButton.onClick.AddListener(TaskOnClick);
    33.         m_YourSecondButton.onClick.AddListener(delegate { TaskWithParameters("Hello"); });
    34.         m_YourThirdButton.onClick.AddListener(() => ButtonClicked(42));
    35.         m_YourThirdButton.onClick.AddListener(TaskOnClick);
    36.         kickSource = GetComponent<AudioSource>();
    37.  
    38.  
    39.  
    40.     }
    41.     void TaskOnClick()
    42.     {
    43.         //Output this to console when Button1 or Button3 is clicked
    44.         Debug.Log("Left mouse clicked");
    45.         float daLatitude = (float)latitude;
    46.         float daLongitude = (float)longitude;
    47.         float daAltitude = (float)altitude;
    48.  
    49.  
    50.         Quaternion thisq = new Quaternion();
    51.         thisq.Set(0, 0, 0, 1);
    52.         Instantiate(prefab, new Vector3(daLatitude, 2.0f,daLongitude), thisq);
    53.         print("clicked by user");
    54.  
    55.     }
    56.     void makethemfloat()
    57.     {
    58.  
    59.  
    60.        
    61.     }
    62.  
    63.     void TaskWithParameters(string message)
    64.     {
    65.         //Output this to console when the Button2 is clicked
    66.         Debug.Log(message);
    67.     }
    68.  
    69.     void ButtonClicked(int buttonNo)
    70.     {
    71.         //Output this to console when the Button3 is clicked
    72.         Debug.Log("Button clicked = " + buttonNo);
    73.     }
    74.  
    75.  
    76.  
    77.  
    78.  
    79. }

    and


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LocativeTarget : MonoBehaviour
    6. {
    7.  
    8.     [Header("Google Maps Coordinates")]
    9.     public double latitude;
    10.     public double longitude;
    11.     public double altitude;
    12.  
    13.     //[Header("Enable")]
    14.     //public float MaximumDistance=1000;
    15.  
    16.     private double raioTerra = 6372797.560856f;
    17.  
    18.     private void Start()
    19.     {
    20.         //print(raioTerra + " " + longitude);
    21.  
    22.     }
    23.  
    24.  
    25.     private void Update()
    26.     {
    27.         Vector3 coord = calculaPosCoordCam();
    28.         transform.position = coord;
    29.         //print(coord.x +" " + coord.y + " " + coord.z);
    30.  
    31.     }
    32.  
    33.  
    34.  
    35.  
    36.  
    37.     private Vector3 calculaPosCoordCam()
    38.     {
    39.         // a dif de long varia consoante e lat
    40.         // isto só dá assim com distâncias pequenas
    41.         //Vector3 p = new Vector3();
    42.  
    43.         double dlat = latitude - LocativeGPS.Instance.latitude;
    44.         dlat = getCoordEmMetrosDeRaio(raioTerra, dlat);
    45.  
    46.         double dlon = longitude - LocativeGPS.Instance.longitude;
    47.  
    48.         double raioLat = Mathf.Cos((float)latitude) * raioTerra;
    49.         dlon = getCoordEmMetrosDeRaio(raioLat, dlon);
    50.  
    51.  
    52.         double dalt = altitude - 0.0f;// LocativeGPS.Instance.altitude;
    53.         return new Vector3((float)dlat, (float)dalt, (float)dlon);
    54.     }
    55.  
    56.  
    57.  
    58.     private double getCoordEmMetrosDeRaio(double raio, double angulo)
    59.     {
    60.         double metros = (raio / 180) * Mathf.PI;// quantos metros tem um grau para aquele lat
    61.         metros *= angulo;
    62.         return metros;
    63.     }
    64.  
    65.  
    66. }