• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Vagonn · Apr 25, 2016 at 07:11 AM · accelerationlocationgpscalculationsspeedometer

Unity3D - Get smooth speed and acceleration with GPS data

Hi, I created simple distance, speed and acceleration calculator using latitude and longitude of last position. I'm calculating last distance, speed and acceleration in each GPS update (approximately once per second). But sometimes (2-3 second interval) latitude and longitude values changes rapidly (in clear weather and no obstacles). That's why speed and acceleration values gets unreal results. For example, at stable 40 km/h speed, speed value becomes 60 km/h and returns to 40 km/h within 2-3 second. I'm here to ask how can I avoid this inaccurate and rapid GPS data changes?

I'm using Nexus 5 device

There is my code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class Manager : MonoBehaviour
 {
 public Text longitude, latitude, lonAText, latAText, lonBText, latBText;
 public Text result, overallResult, speedText, lastTimeText, timerText, accelerationText, speed0Text;
 
 float lonA, lonB, latA, latB, overallDistance, lastDistance, timer, lastTime, speed, speed0, acceleration;
 bool firstTime, allowTimer;
 
 public AudioSource audio;
 
 void Awake()
 {
     overallDistance = 0;
     lastDistance = 0;
     timer = 0;
     lastTime = 0;
     speed = 0;
     speed0 = 0;
 
     firstTime = true;
     allowTimer = true;
 }
 
 IEnumerator Start()
 {
     // First, check if user has location service enabled
     if (!Input.location.isEnabledByUser)
         yield break;
 
     // Start service before querying location
     Input.location.Start(1, 1);
 
     // Wait until service initializes
     int maxWait = 20;
     while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
     {
         yield return new WaitForSeconds(1);
         maxWait--;
     }
 
     // Service didn't initialize in 20 seconds
     if (maxWait < 1)
     {
         print("Timed out");
         yield break;
     }
 
     // Connection has failed
     if (Input.location.status == LocationServiceStatus.Failed)
     {
         print("Unable to determine device location");
         yield break;
     }
     else
     {
         // Access granted and location value could be retrieved
         print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
 
         longitude.text = Input.location.lastData.longitude.ToString();
         latitude.text = Input.location.lastData.latitude.ToString();
 
         lonA = Input.location.lastData.longitude;
         latA = Input.location.lastData.latitude;
     }
 
     // Stop service if there is no need to query location updates continuously
     //Input.location.Stop();
 }
 
 void Update()
 {
     longitude.text = Input.location.lastData.longitude.ToString();
     latitude.text = Input.location.lastData.latitude.ToString();
 
     timer += Time.deltaTime;
     timerText.text = timer.ToString();
 
 
     if (lonA != Input.location.lastData.longitude || latA != Input.location.lastData.latitude)
     {
         audio.Play();
 
         CalculateDistances(lonA, latA, Input.location.lastData.longitude, Input.location.lastData.latitude);  // last distance and overall distanceS            
         lonA = Input.location.lastData.longitude;
         latA = Input.location.lastData.latitude;
 
         lastTime = timer;
         lastTimeText.text = lastTime.ToString();
         timer = 0;
 
 
         speed0 = speed;
         speed0Text.text = speed0.ToString();
 
         CalculateSpeed();
 
         CalculateAcceleration();
     }            
 }   
 
 public static float Radians(float x)
 {
     return x * Mathf.PI / 180;
 }
 
 public void CalculateDistances(float firstLon, float firstLat, float secondLon, float secondLat)
 {
     lonAText.text = firstLon.ToString();
     latAText.text = firstLat.ToString();
 
     lonBText.text = secondLon.ToString();
     latBText.text = secondLat.ToString();
 
     float dlon = Radians(secondLon - firstLon);
     float dlat = Radians(secondLat - firstLat);
 
     float distance = Mathf.Pow(Mathf.Sin(dlat / 2), 2) + Mathf.Cos(Radians(firstLat)) * Mathf.Cos(Radians(secondLat)) * Mathf.Pow(Mathf.Sin(dlon / 2), 2);
 
     float c = 2 * Mathf.Atan2(Mathf.Sqrt(distance), Mathf.Sqrt(1 - distance));
 
     lastDistance = 6371 * c * 1000; 
 
     result.text = lastDistance.ToString() + " meters";
 
     overallDistance += lastDistance;  // bu 1 anliq 6.000.000-dan boyuk qiymet ala biler
 
     StartCoroutine(Overall());
 }
 
 IEnumerator Overall()
 {
     if (firstTime)
     {
         firstTime = false;
 
         yield return new WaitForSeconds(2);
 
         if (overallDistance > 6000000)
         {
             overallDistance = 0;
             lastDistance = 0;
         }
     }
 
     overallDistance += lastDistance;
     overallResult.text = overallDistance.ToString() + " meters";
 }
 
 void CalculateSpeed()
 {
     speed = lastDistance / lastTime * 3.6f;
 
     speedText.text = speed.ToString();
 }
 
 void CalculateAcceleration()
 {
     acceleration = (speed - speed0) / lastTime;
     accelerationText.text = acceleration.ToString();
 }
 }

Thanks for any help and tip

PS. Please edit my question if I made any grammar and logical mistake

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sovan-k1 · Jun 09, 2017 at 02:38 PM

Hello @Vagonn did you get to know the exact reason of the inaccuracy? Please help me if you have already got the answer.

Thanks,

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by SimonAlkemade · Jun 28, 2018 at 10:04 AM

Have you tried lerping or smooth damping the speed values?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get Real GPS not WiFi GPS 2 Answers

GPS location string 0 Answers

GPS coordinates (lat/long/altitude) to Unity 3D coordinates (x, y, z) 1 Answer

Is There A Way To Use GPS Data On A Mac Stand Alone build? 1 Answer

Any Augmented Reality SDK plugin with Geo-Location functions? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges