Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by jjchoio · Dec 01, 2012 at 09:01 PM · 2ddata

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,

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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
     }
 }

Comment
Add comment · Show 7 · 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 jjchoio · Dec 01, 2012 at 09:43 PM 0
Share

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!!

avatar image Eric5h5 · Dec 01, 2012 at 09:46 PM 2
Share

Would be simpler/more efficient to use InvokeRepeating rather than Update for this.

avatar image jjchoio · Dec 01, 2012 at 09:51 PM 0
Share

thanks! I will look into InvokeRepeating

avatar image aldonaletto · Dec 01, 2012 at 11:55 PM 1
Share

Take a look at my answer: I edited it to use InvokeRepeating and included a SaveToFile function

avatar image aldonaletto · Dec 02, 2012 at 02:12 PM 1
Share

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.

Show more comments

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.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Texture2D.GetTextureRawData doesnt work outside the editor ? 1 Answer

how i can write a text from another code ? 2 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

2D Animation does not start 1 Answer

How do I interpolate within a 2d dataset? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges