简体   繁体   中英

Move an object with a list of points from a text file

I am trying to move my Cube from point to point where the coordinates are from a text file.

public class cube : MonoBehaviour
{
    // Speed
    public float speed = 3.0f;

    // Start is called before the first frame update
    void Start()
    {
       print("cube says hi");

    }

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

        string path = "Assets/Ressources/test.txt";
        var sr = new StreamReader(path);
        List<string> columnx = new List<string>();
        List<string> columny = new List<string>();
        List<string> columnz = new List<string>();

        using (sr)
        {
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                var values = line.Split(new string[] { "      " }, System.StringSplitOptions.RemoveEmptyEntries);

                columnx.Add(values[0]);
                columny.Add(values[1]);
                columnz.Add(values[2]);

            }


        }


        float step = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position,
                                            new Vector3(
                                                Convert.ToSingle("1.45", CultureInfo.InvariantCulture), Convert.ToSingle("3.258", CultureInfo.InvariantCulture), Convert.ToSingle("4.256", CultureInfo.InvariantCulture)
                                            ), step);

    }

}

this works, but the problem is when I replace "1.45" "3.25" and "4.25" by columnx[0] columny[0] and columnz[0] I got

FormatException: Input string was not in a correct format.
System.Number.ParseSingle (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) (at <437ba245d8404784b9fbab9b439ac908>:0)

I wanted to test with the first element so I can make a for loop but it's not even working with 0..

I solved the problem. I just put one blank space for split instead of 5 or 6 (which because depends if theres a - or not).., I printed my columns: they are all working ! thanks for that ! But now I am trying to move my object from vector to vector with a for loop :

 for (int i =0; i< columnx.Count ; i++)
        {

            position = new Vector3(Convert.ToSingle(columnx[i], CultureInfo.InvariantCulture), Convert.ToSingle(columny[i], CultureInfo.InvariantCulture), Convert.ToSingle(columnz[i], CultureInfo.InvariantCulture));


            transform.position = Vector3.MoveTowards(currentPosition, position, step);


        }

but looks like the cube is instantly going at the last point

I suppose you have a file like this with tab, space between number

        0.10340200    0.01262700   0.46301100



    0.10340200        0.01262700 0.46301100        
        0.10340200 0.01262700 0.46301100         

i suggest you to use a List of Vector3 directly and to Convert to Float and not single, because Vector3 is a vector of 3 float, so if you convert to single, another conversion is done again (and you loose precision)..

    List<Vector3> vec = new List<Vector3>();

    string path = "Assets/file.txt";

    var fileLines = System.IO.File.ReadAllLines(path);
    foreach (var line in fileLines)
    {
        var result = line.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
        if (result.Length == 3)
        {
            var x = float.Parse(result[0], CultureInfo.InvariantCulture);
            var y = float.Parse(result[1], CultureInfo.InvariantCulture);
            var z = float.Parse(result[2], CultureInfo.InvariantCulture);
            vec.Add(new Vector3(x, y, z));
        }

    }

    float step = speed * Time.deltaTime;
    transform.position = Vector3.MoveTowards(transform.position,
                                        vec[0], step);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM