Use Lerp + Animation Curve Move AI along Path

For a little mini game, I want to move a sprite(bug) from flower to flower in a bug like fashion.
Like this image:[49262-bug-mover.jpg*|49262]

but I have just had so many errors, and so may issues that I just had to ask how someone would do it. Here is my entire code:

    [SerializeField] private Transform bug;
    [SerializeField] private Transform flowers;
    private Transform[] targets;
    [SerializeField] private AnimationCurve tween;

    public float WaitAtFlowerTime = 2f;
    public int currentFlowerID = 0;
    public float reachDistance = 350;
    private float startingDistance;

    public bool drawGizmos = false;

    public Vector3 GetNodePos(int id)
    {
        return targets[id].localPosition;
    }

    private Vector3 orig;
    public float Speed;

    private void Start()
    {
        targets = new Transform[flowers.transform.childCount];
        for(int i = 0; i < flowers.transform.childCount; i++)
        {
            targets *= flowers.transform.GetChild(i).transform;*

}
orig = bug.transform.localPosition;
}

private void Update()
{
Vector3 dest = GetNodePos(currentFlowerID);
Vector3 offset = dest - bug.transform.localPosition;
if (offset.sqrMagnitude > reachDistance)
{
offset = offset.normalized;
bug.transform.Translate(offset * Speed * Time.deltaTime, Space.World);
bug.transform.localPosition = new Vector3(bug.transform.localPosition.x, new AnimationCurve(new Keyframe(0, orig.y), new Keyframe(0.5f, (offset.y + 70f)), new Keyframe(1, orig.y)).Evaluate(Speed * Time.deltaTime), bug.transform.localPosition.z);
}
else
{
ChangeDestFlower();
}
}

private void ChangeDestFlower()
{
orig = targets[currentFlowerID].position;
currentFlowerID++;
if (currentFlowerID >= targets.Length)
{
currentFlowerID = 0;
}
}
The current error I have makes the bug fly around like mad, and then hover on the Y axis at 0 directly above a flower.
Anyone have any ideas?
*

You don’t use the “tween” variable. Maybe you wanted to use this instead of creating a new curve all the time? (And you don’t use WaitAtFlowerTime, but I guess that was just work-in-progress)

However, the main bug is probably that you don’t really pass any time value into the Evaluate. You just take the constant Speed multiply by the Time.deltaTime. What you have to pass is a float between 0 and 1. The usual Hack for “lerping to a target by just moving with Time.deltaTime * speed” - which is terrible, btw - doesn’t work here, as you do not constantly approach the target

Basically the correct way to do a lerp if the source and target is non-moving:

  • Remember the position before you start moving
  • Every frame, calculate the percentage of the point in time you are (between 0 and 1)
  • use Vector3.Lerp to linear interpolate or SmoothDamp (if you want the ease-out effect) within “t”.

This way, you also don’t need the “reachDistance” as you already know when you arrived.

Another thing: Seeing your reachDistance of “350” and y-offset of “70”, I guess you use 1 pixel = 1 Unit. Don’t do that. Use KMS system whenever possible (kilogram, meter, seconds). This will ease your pain with float inaccuracy later in development when it might be too late to switch back. (To give you a hint about the severity: Unity starts to warn you for transform coordinates >100000