1

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.

  • What do you want to reach with this ? Because if you put the first script on a GameObject and set it's Vectors and AnimCurves to something different then 0 this should move the Object – Assasin Bot Jan 29 '20 at 22:12
  • I guess I am struggling with which values to insert into the Keyframe.Add parameters. Do I get the the change speed by using the change in their positions as the Value to add or something else maybe? For example when the car would drive down the road and turn right, the change in the x and z values over time would change in the AnimationCurve right? – Brian Mynhardt Jan 29 '20 at 22:19
  • Not sure if this helps, but GoKit apparently has built-in support for tweening a transform along a given path (https://github.com/prime31/GoKit/wiki/4.-Tweening-Along-a-Path-or-Spline). Your max seconds time for the animation would be steps * 0.1. (And if you need a physical rigidbody feel, maybe you could calculate the velocities for each point given your time and position data, and Time.deltaTime in a FixedUpdate Vector3.Lerp between them and assign ForceMode.VelocityChange.) – Philipp Lenssen Jan 29 '20 at 22:45
  • And how about moving the `foreach` inside of the coroutine then? Also not sure why in the routine you would first `WaitforSeconds` and then do your transition? In general it sounds like you would rather want to use the `Animator` or `Timeline` .. why doing this in a script? – derHugo Jan 30 '20 at 07:03

0 Answers0