- Home /
extract & calculate position Data
Hello! Thank you for helping in advance, I am pretty new to unity and C# scripting.
First, I would like to extract position data of a character's movement. (how do you usually extract data? excel..?)
And wish to use the data to draw a 2D map of where the character had moved before. OR, calculate where the character went the most.
Please, help if you can! thank you for reading,
Answer by aldonaletto · Dec 01, 2012 at 09:20 PM
I would save the positions in a List - the data could then be saved to a text file, if you want to export it, or processed in your game. If you want to know how much time each position is visited, it's better to sample positions at a constant rate. You could attach a script like this one to the character:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RecPositions : MonoBehaviour {
public List<Vector3> positions;
public float interval = 0.1f; // save positions each 0.1 second
public float tSample = 10.0f; // sampling starts after this time
void Start(){
positions = new List<Vector3>(); // initialize it
}
void Update(){
if (Time.time > tSample){ // if it's time to sample...
positions.Add(transform.position); // sample position...
tSample += interval; // and set new sample time
}
}
}
EDITED: @Eric5h5 gave us a good suggestion: InvokeRepeating is more efficient and precise for timed functions. About saving to text: you can create/rewind the text file and enter a foreach loop to save all positions in some suitable text format. You could for instance use a semicolon to separate the XYZ coordinates, and skip a line to separate points - this can be easily read in Excel, Access etc.
That's how the code could be:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RecPositions : MonoBehaviour {
public string fileName = "C:/positions.txt"; // file pathname
public float interval = 0.1f; // save positions each 0.1 second
public float tSample = 10.0f; // sampling starts after this time
private List<Vector3> positions;
void Start(){
positions = new List<Vector3>(); // initialize the array...
// and start recording after tSample:
InvokeRepeating("RecPoint", tSample, interval);
}
void RecPoint(){
positions.Add(transform.position); // store position...
}
// function that saves to a text file:
void SaveToFile(string fileName){
System.IO.File.WriteAllText(fileName, ""); // clear old file, if any
foreach (Vector3 pos in positions){
// format XYZ separated by ; and with 2 decimal places:
string line = System.String.Format("{0,3:f2};{1,3:f2};{2,3:f2}\r\n", pos.x, pos.y, pos.z);
System.IO.File.AppendAllText(fileName, line); // append to the file
}
}
// example of use:
void OnGUI(){
if (GUI.Button(new Rect(10,10,120,30), "Save")){
CancelInvoke("RecPoint"); // stop recording
SaveToFile(fileName); // save positions
}
}
}
EDITED 2: This is the JS version:
import System.Collections.Generic;
var fileName = "C:/positions.txt"; // file pathname
var interval: float = 0.1f; // save positions each 0.1 second
var tSample: float = 10.0f; // sampling starts after this time
private var positions: List.<Vector3>; // generic functions have a . before the type in JS
function Start(){
positions = new List.<Vector3>(); // initialize the array...
// and start recording after tSample:
InvokeRepeating("RecPoint", tSample, interval);
}
function RecPoint(){
positions.Add(transform.position); // store position...
}
// function that saves to a text file:
function SaveToFile(fileName: String){
System.IO.File.WriteAllText(fileName, ""); // clear old file, if any
for (var pos: Vector3 in positions){
// format XYZ separated by ; and with 2 decimal places:
var line = System.String.Format("{0,3:f2};{1,3:f2};{2,3:f2}\r\n", pos.x, pos.y, pos.z);
System.IO.File.AppendAllText(fileName, line); // append position to the file
}
}
// example of use:
function OnGUI(){
if (GUI.Button(new Rect(10,10,120,30), "Save")){
CancelInvoke("RecPoint"); // stop recording
SaveToFile(fileName); // save positions
}
}
thank you! so.. just to be clear, does your code make a list of position data? and how could i save to txt file(if is useful)? sorry for simple questions!!
Would be simpler/more efficient to use InvokeRepeating rather than Update for this.
Take a look at my answer: I edited it to use InvokeRepeating and included a SaveToFile function
Translating this script to JS isn't trivial, but you're lucky: I usually program in JS, and will post a JS version in my answer soon.
NOTE: JS version already posted.