Search Unity

2D Movement with Position Array

Discussion in 'Scripting' started by vanutella, Jan 21, 2020.

  1. vanutella

    vanutella

    Joined:
    Jun 6, 2017
    Posts:
    10
    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!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
  3. vanutella

    vanutella

    Joined:
    Jun 6, 2017
    Posts:
    10
    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...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5.  
    6. public class jsonthingy : MonoBehaviour
    7. {
    8.     public int multiplier;
    9.  
    10.     public float timepassed;
    11.  
    12.     public animationData _animData;
    13.     public int xIndex , yIndex , tIndex;
    14.     string path;
    15.     string jsonString;
    16.     bool dataLoaded = false;
    17.  
    18.     public GameObject Person;
    19.     int tempInt = 0;
    20.  
    21.  
    22.  
    23.     void Update()
    24.     {
    25.         timepassed = timepassed + Time.deltaTime;
    26.       //  Debug.Log(timepassed);
    27.         if (dataLoaded==false)
    28.         {
    29.             LoadData();
    30.         }
    31.  
    32.  
    33.         CheckTime(); // and move
    34.  
    35.     }
    36.  
    37.     //Lerp
    38.     public float speed = 10f;
    39.  
    40.         //moving the person/wagon smoothly
    41.     void LerpWagon()
    42.     {
    43.         Vector2 posA = new Vector2(_animData.x[tempInt - 1]*multiplier, _animData.y[tempInt - 1]*multiplier);
    44.         Vector2 posB = new Vector2(_animData.x[tempInt]*multiplier, _animData.y[tempInt]*multiplier);
    45.  
    46.         Person.transform.position = Vector2.Lerp(posA, posB, speed);
    47.     }
    48.  
    49.     void CheckTime()
    50.     {
    51.         if (tempInt < 75 && timepassed >= _animData.t[tempInt]*multiplier)
    52.         {
    53.          
    54.  
    55.             tempInt = tempInt + 1;
    56.             LerpWagon();
    57.         }
    58.         else if(tempInt>=75)
    59.         {
    60.             Debug.Log("Reached end of Array");
    61.             //moving person to right
    62.             Person.transform.position += transform.right * 10f * Time.deltaTime;
    63.         }
    64.  
    65.        
    66.     }
    67.  
    68.     void LoadData()
    69.     {
    70.         path = Application.streamingAssetsPath + "/animation.json";
    71.         jsonString = File.ReadAllText(path);
    72.         animationData _animationData = JsonUtility.FromJson<animationData>(jsonString);
    73.         for (int i = 0; i < 10; i++)
    74.         {
    75.             Debug.Log(_animationData.t[i]);
    76.         }
    77.         dataLoaded = true;
    78.  
    79.         // put it in variables
    80.         _animData.t = _animationData.t;
    81.         _animData.x = _animationData.x;
    82.         _animData.y = _animationData.y;
    83.         _animData.acc_x = _animationData.acc_x;
    84.         _animData.acc_y = _animationData.acc_y;
    85.     }
    86. }
    87.  
    88.  
    89. [System.Serializable]
    90. public class animationData
    91. {
    92.     public float[] acc_x;
    93.     public float[] acc_y;
    94.     public float[] angle;
    95.     public float[] t;
    96.     public float[] x;
    97.     public float[] y;
    98. }
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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)