For my research, I need to build a dual-task protocol involving driving simulator. So far I have already created the driving track, the scene, and the car. However, I’m having problems understanding and seeing how I can get data back from the game.
I need the ability (1) to extract the location of the car in real time and evaluate participants’ performance (i.e., deviation from the middle of the track), and (2) to feed GPS instruction to participants in real time based on the location of the car (I suppose the first point is the most important one).
I’m new to Unity and has no experience in C# programming language, so I was wondering if anyone can provide me with some direction.
The location of the car would be the car GameObject’s transform.position. If you use the recommended scale, then 1 unit = 1 meter.
To get deviation from the middle of the track will be more tricky and there’s several ways to approach it. You could create a series of points down the middle of the track, put them in a list, then loop through the list calculating the distance from each point to the car’s position. The lowest result would be the actual distance from the middle. That would be fairly inefficient though, but might be fine if you don’t need this information every frame.
As far as feeding GPS instructions, well that depends on what your GPS system does. Unity doesn’t have anything like a GPS system built in, so it would be something you create, and how you design it will largely dictate how its data should be presented to the player.
I was hoping to measure the car position and to output to a text file at regular intervals as the player drives, but I wasn’t sure how this can be done.
In regards to the GPS instructions, I was wondering if it will be possible to create using C# script and then use the audio function in unity to present the instruction to the player. The GPS instruction would be something like “Turn Left”, “Turn Right”. But these instructions will need to be presented based on the real time location of the car.
For the offset from the centre of the track, have a large series of positions along it stored in an array. At the start of the track, triangulate the 1st and 2nd points on the array with the car. The height of the triangle will give the offset from the centre. When the 3rd point in the array is closer to the car that the 1st, switch to triangulating the 2nd and 3rd. Repeat throughout the array.
As I understand it you want to display GPS instructions related to position in a virtual world? If so, just convert the x and z positions into a suitable format and show on the UI.
I guess I mean the users actual GPS location. I have created the road network using ‘EasyRoad3d’. What I need is to output the car location in real time (as the player drive) so that during the game it can calculate the distance between the car and the next road crossing and give the suitable GPS instruction (audio format) in the suitable time.
I’m planning to obtain the GPS location in the game (from the road network map that we will create), not the user’s location, as you said the user will be sitting in the same location when playing the game on the PC.