Search code examples
c#androidunity-game-enginegps

How to move a Player in Vector3 using GPS coordinates? [Unity]


I need to develop a Unity Android APP to help in agricultural guidance and I am facing some very especific GPS problems, let me just explain the project to you.

  1. It needs to use accurate GPS data to guide you on the fields
  2. It won't be able to connect the ethernet, I just get the Latitude and Longitude, the map is just a infinite ground plane (already made) not some real world map.
  3. The "player" (tractor, machine,...) needs to move on the Unity space (not the ground texture, the player itself) because I need to draw a Trail Renderer for my planting path (already made).

So I have a very specific situation where every API I found moves the ground texture instead of the player object. I need to transform my Latitude and Longitude changes (movement) into Unity space movement, I need to accurately move (transform.Translate) my player on the Vector3 coordinates every time the GPS coordinates changes. This code is about what I have right now.

//so in the Update method
if (newLat != lastLat || newLong != lastLong){
  //what do I do here?

EDIT 1: This guy has the same question as mine on an old post, but got no response.


Solution

  • After reading this code, I think you can just get the latitude and the longitude and set the position of an object, not move.

    By that I mean something like this:

    // Get the GPS values (I'm not sure if you can do it like this, I copied it from the code you linked)
    latitude = Input.location.lastData.latitude;
    longitude = Input.location.lastData.longitude;
    
    // Set the position of a GameObject using the GPS values
    transform.position = new Vector3(latitude , 0, longitude);