Problems moving object along a vector with Vector3.Lerp

Hi,

I’m having an issue with moving an object along a vector. Hopefully it’s just something stupid where I’m not understanding vectors and points.

    IEnumerator MoveToPoint(Vector3 startPos, Vector3 targetPos, float time)
    {
        Debug.DrawRay(startPos, targetPos, Color.red, 1.0f);
        drawTarget.position = targetPos;

        float i = 0;
        float rate = 1 / time;
        while (i < 1f)
        {

            i += Time.deltaTime * rate;
            transform.position = Vector3.Lerp(startPos, targetPos, i);
            yield return null;
        }
    }

The problem here is that the ray being draw from Debug.DrawRay looks correct. That’s the path I want the object to follow. BUT the target object (drawTarget) I’m using just to test shows up at a different spot offset from the vector. And the object I’m moving goes to that point instead of along the vector.

Can anyone point out what I might be doing wrong? Thank you!

Edit: For the moment, got around it by changing from Vector3.Lerp to Transform.translate which seems to work fine. I still don’t understand why the drawTarget object doesn’t just get moved to the same position as the end of that DrawRay in scene view.

The script isn’t wrong, it’s about how you call the DrawRay method. The second parameter of Debug.DrawRay should be the direction of the ray, so it works if you replace the line in your script with

Debug.DrawRay(startPos, targetPos - startPos, Color.red, 1.0f);

Note that Debug.DrawLine does use another point instead of a direction, though.