1
\$\begingroup\$

I have created a bezier curve by adding the following script to an empty game object in the inspector. This draws to complete curve at once when I run the code. How can I animate it over a given period of time, say 2 or 3 seconds?

public class BCurve : MonoBehaviour {

LineRenderer lineRenderer;
public Vector3 point0, point1, point2;
int numPoints = 50;
Vector3[] positions = new Vector3[50];

// Use this for initialization
void Start () {
    lineRenderer = gameObject.AddComponent<LineRenderer>();
    lineRenderer.material = new Material (Shader.Find ("Sprites/Default"));
    lineRenderer.startColor = lineRenderer.endColor = Color.white;
    lineRenderer.startWidth = lineRenderer.endWidth = 0.1f;
    lineRenderer.positionCount = numPoints;

    DrawQuadraticCurve ();

}

void DrawQuadraticCurve ()  {
    for (int i = 1; i < numPoints + 1; i++) {
        float t = i / (float)numPoints;
        positions [i - 1] = CalculateLinearBeziearPoint (t, point0, point1, point2);

     }
    lineRenderer.SetPositions(positions);
}

 Vector3 CalculateLinearBeziearPoint (float t, Vector3 p0, Vector3 p1,    Vector3 p2)   {

    float u = 1 - t;
    float tt = t * t;
    float uu = u * u;
    Vector3 p = uu * p0 + 2 * u * t * p1 + tt * p2;

    return p;
}
\$\endgroup\$
1
\$\begingroup\$

You're doing all the work at once. You could do this in Update() and just render the points one by one, or you could reveal it slowly, by setting it to an image and then revealing it over a linear interpolation.

There's also one more method, you could just not draw the bezier curve from your calculations, but instead make a gameobject (preferably invisible) follow it. Attaching a trail renderer to the gameobject will give you a nice drawing effect, which you can adjust all sorts of ways.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.