0
\$\begingroup\$

What I want is to get path for DoTween method DoPath(), which as a parameter expects array of vectors shaping the direction of the path. I googled some stuff and found out nothing. Just think bezier curve is good idea but i dont even know how it works. Here is an ilustration of my problem enter image description here

The movement must be curved, don't need linear one. Thanks in advance

\$\endgroup\$
2
  • 1
    \$\begingroup\$ We're missing one important input: how the line should curve. There's an infinite variety of potential paths connecting point 1 & point 2 - so your game code needs to somehow specify which one is the one you want. One way this is often done is to add a third point, a "control point" that acts like a magnet to pull the curve away from a straight line. Would that work for your case? In fact, if you can provide two such magnets - one to set the direction at the start, one to set the direction at the end - you can use the CubicBezier mode built-into DoTween instead of a whole lot of waypoints. \$\endgroup\$
    – DMGregory
    Commented Jun 1, 2020 at 20:07
  • \$\begingroup\$ I've solved my problem. thx for ur responce \$\endgroup\$
    – Chestera
    Commented Jun 1, 2020 at 22:26

1 Answer 1

0
\$\begingroup\$

So, I have found this article about bezier curves, which helped me a lot. This is code I wrote, hope it'll useful for someone

public static Vector3[] GetQuadraticBezierPoints(Vector3 startpoint, Vector3 endPoint, float curveHeigh) {
    Vector3 heighPoint = startpoint + (endPoint - startpoint) / 2 + Vector3.up * curveHeigh;

    Vector3[] res = new Vector3[100];
    int maxT = 1;
    int index = 0;

    for (float t = 0; t <= maxT; t += 0.01f) {
        Vector3 newPoint = (Mathf.Pow(1 - t, 2) * startpoint) + (2 * (1 - t) * t * heighPoint) + (t * t * endPoint);
        try {
            res[index++] = newPoint;
        }
        catch {
            break;
        }
    }
    return res;
}

This script creates 100 points, shaping curve, based on curveHeigh value, which also helps to create heigPoint variable, which is a 'control point'

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .