I have a large dataset that contains the position data for a vehicle at 0.1 second intervals and I have been trying to take this data and animate a gameobject smoothly along these positions.
I have tried Using AnimationCurves and adding keyframes for the individual axis similar to this
public AnimationCurve ac;
public AnimationCurve ac1;
public Vector3 pos1 = new Vector3(0.0f, 0.0f, 0.0f);
public Vector3 pos2 = new Vector3(0.0f, 0.0f, 0.0f);
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Move(pos1, pos2, ac, ac1, 3.0f));
}
}
IEnumerator Move(Vector3 pos1, Vector3 pos2, AnimationCurve ac, AnimationCurve ac1, float time)
{
float timer = 0.0f;
while (timer <= time)
{
var x = Mathf.Lerp(pos1.x, pos2.x, ac.Evaluate(timer / time));
var y = Mathf.Lerp(pos1.y, pos2.y, ac1.Evaluate(timer / time));
transform.position = new Vector3(x, y, 0);
timer += Time.deltaTime;
yield return null;
}
}
However I couldnt figure out how to turn the splitsecond positional data into the correct values for the AnimationClip.
I also tried using looping over the positions with coroutines similar to this:
var coords = vehicles[0].middleCoords;
var previousStep = coords[0];
foreach(Vector3 step in coords)
{
MoveOverSeconds(step, 0.1f);
}
public IEnumerator MoveOverSeconds(Vector3 end, float seconds)
{
yield return new WaitForSeconds(seconds);
float elapsedTime = 0;
Vector3 startingPos = transform.position;
while (elapsedTime < seconds)
{
transform.position = Vector3.Lerp(startingPos, end, (elapsedTime / seconds));
elapsedTime += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
transform.position = end;
However because the for loop runs independent of time it starts the co-routine before the other one ends. Any help on how to solve this problem would be greatly appreciated.