2D Movement with Position Array

Hey!
I got a file with some data that contains velocity, time and position values for an object. (acc_x, acc_y, t, pos_x, pos_y, so this is 2d!)
What I need to do is move the object smoothly according to the data.
i tried to move the object by checking the time that has passed and comparing it to the data.
then i count up the index of the position arrays, so i know where the object has to be. but when i use lerp, it still doens’t move smoothly…
Maybe I’m doing it way too difficult and complicated, so please let me know if you know of anything that i can use, so it’s moving smoothly and still accurate! let me know if you want to read the code.

thank you!

Sounds similar to how I’d approach the problem. Show us the code you have (in code tags), you may be using something a little bit wrongly.

So this is my code. I set the multiplier to 10 because the values were too small to work with. i was playing around with the speed and set it to 0.5 but also tried 200. didnt seem to work…

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

public class jsonthingy : MonoBehaviour
{
    public int multiplier;

    public float timepassed;

    public animationData _animData;
    public int xIndex , yIndex , tIndex;
    string path;
    string jsonString;
    bool dataLoaded = false;

    public GameObject Person;
    int tempInt = 0;


  
    void Update()
    {
        timepassed = timepassed + Time.deltaTime;
      //  Debug.Log(timepassed);
        if (dataLoaded==false)
        {
            LoadData();
        }


        CheckTime(); // and move

    }

    //Lerp
    public float speed = 10f;
  
        //moving the person/wagon smoothly
    void LerpWagon()
    {
        Vector2 posA = new Vector2(_animData.x[tempInt - 1]*multiplier, _animData.y[tempInt - 1]*multiplier);
        Vector2 posB = new Vector2(_animData.x[tempInt]*multiplier, _animData.y[tempInt]*multiplier);

        Person.transform.position = Vector2.Lerp(posA, posB, speed);
    }

    void CheckTime()
    {
        if (tempInt < 75 && timepassed >= _animData.t[tempInt]*multiplier)
        {
          

            tempInt = tempInt + 1;
            LerpWagon();
        }
        else if(tempInt>=75)
        {
            Debug.Log("Reached end of Array");
            //moving person to right
            Person.transform.position += transform.right * 10f * Time.deltaTime;
        }

       
    }

    void LoadData()
    {
        path = Application.streamingAssetsPath + "/animation.json";
        jsonString = File.ReadAllText(path);
        animationData _animationData = JsonUtility.FromJson<animationData>(jsonString);
        for (int i = 0; i < 10; i++)
        {
            Debug.Log(_animationData.t[i]);
        }
        dataLoaded = true;

        // put it in variables
        _animData.t = _animationData.t;
        _animData.x = _animationData.x;
        _animData.y = _animationData.y;
        _animData.acc_x = _animationData.acc_x;
        _animData.acc_y = _animationData.acc_y;
    }
}


[System.Serializable]
public class animationData
{
    public float[] acc_x;
    public float[] acc_y;
    public float[] angle;
    public float[] t;
    public float[] x;
    public float[] y;
}

Little things that aren’t the main problem:

  1. You could use _animData directly on line 72, and then you wouldn’t need lines 80-84.
  2. It’s easier to read code if you use naming conventions in line with other code/communities, e.g. the Unity code base. Specifically, UpperCaseNames for class names, like AnimationData.

Your actual problem here: line 46. Lerp is something that a lot of people misunderstand, particularly the third parameter. You’re currently sending it a value of 10 all the time. You need to give it a number between 0 and 1. For 0 it will return posA, for 1 it will return posB, 0.5 will return a value halfway in between, etc. I think you want to use timepassed / _animData.t[tempInt] for that. (and I don’t think you want multiplier to affect that value, either there or on line 51)