1
\$\begingroup\$

I'm trying to track playing position every 500ms which i can do but i need it to populate an array which i save to a txt file as a comma delimted list or something.

I have this code so far (which incorporates the player movement code too), which displays the positions in the console:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterControllerT : MonoBehaviour {

    public float speed = 10.0f;
    public Read_moveT ReadMove;
    [SerializeField] private float _duration = 0.5f;
    private float _timer = 0f;
    public Vector3 PlayerPosT;
    GameObject Mover;
    public Vector3[] PlayerTxyz;

    // Use this for initialization
    void Start () {
        Cursor.lockState = CursorLockMode.Locked;
        Mover = GameObject.Find ("PlayerT");
        ReadMove = Mover.GetComponent<Read_moveT> ();
    }

    // Update is called once per frame
    void Update () {

        if (!Input.GetButton ("DoorTrigger")) {
            ReadMove.mUP = Input.GetAxis ("Vertical");
            ReadMove.mLEFT = Input.GetAxis ("Horizontal");
            float translation = ReadMove.mUP * speed;
            float straffe = ReadMove.mLEFT * speed;

            translation *= Time.deltaTime;
            straffe *= Time.deltaTime;

            transform.Translate (straffe, 0, translation);
        }

        if (Input.GetKeyDown ("escape")) {
            Cursor.lockState = CursorLockMode.None;
        }


        _timer += Time.deltaTime;
        if (_timer >= _duration) {
            _timer = 0f;
            PlayerPosT = Mover.transform.position;
            print ("Player:: X: " + PlayerPosT.x + ", Z: " + PlayerPosT.z);
        }
    }
}

I just need these coords to be saved into an array that i can process when the game is over. The problem being that the array will always be (until game over) of undetermined length.

Any ideas?

Thanks,

Paul..

\$\endgroup\$
2
  • \$\begingroup\$ Have you considered a List<Vector3>? These can grow as needed to accommodate newly Add()ed entries. \$\endgroup\$
    – DMGregory
    May 13 '17 at 11:39
  • \$\begingroup\$ Thanks @DMGregory. I didn't know about Lists - i'm new to c# :) \$\endgroup\$ May 15 '17 at 16:12
1
\$\begingroup\$

You can initialize a List<Vector3> and then when you need to save the position just call it with the List.Add(Vector3) like the following. Note that saving process better be done outside of your CharacterControllerT script in order not to lose the saved positions if you destroy the character or something.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PositionRecorder : MonoBehaviour {

    private static List<Vector3> savedPositions;

    // Use this for initialization
    void Start () {
        savedPositions = new List<Vector3>();
    }

    public static void SavePosition(Vector3 position){
        savedPositions.Add(position);
    }
}

Then call it from outside like positionRecorder.SavePosition(PlayerPosT)

\$\endgroup\$
3
  • \$\begingroup\$ Thanks @Tolga Sen, I adapted a bit of your code to suit my needs and it works perfectly :) I just need to work out now how to create a line that joins the dots to create the path in a visual way but i'm sure i can work that out or might feed it to AutoCAD and create a polyline with the coords. \$\endgroup\$ May 15 '17 at 16:14
  • \$\begingroup\$ You are welcome, if you are stuck on that you can use LineRenderer component of unity and set the "points" field of LineRenderer object with your savedPositions. Check out manual: docs.unity3d.com/Manual/class-LineRenderer.html and draw that wherever you like \$\endgroup\$
    – Tolga Şen
    May 15 '17 at 17:36
  • \$\begingroup\$ One last comment, would you upvote the answer if that has worked for you? \$\endgroup\$
    – Tolga Şen
    May 15 '17 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.