I want to make a smooth line from point A to point B similar to what present between the nodes of bolt graph…
I want to use line renderer but the main problem is how to get some points which will make that kind of line possible…
2 Answers
2One simple way is to use 4-point Bezier curves. They are pretty easy to calculate:
Vector2 Bezier(Vector2 a, Vector2 b, float t) {
return Vector2.Lerp(a, b, t);
}
Vector2 Bezier(Vector2 a, Vector2 b, Vector2 c, float t) {
return Vector2.Lerp(Bezier(a, b, t), Bezier(b, c, t), t);
}
Vector2 Bezier(Vector2 a, Vector2 b, Vector2 c, Vector2 d, float t) {
return Vector2.Lerp(Bezier(a, b, c, t), Bezier(b, c, d, t), t);
}
These functions define curves using a number of control points (a, b, c and d). You feed them an interpolation parameter t, which starts at 0 and ends at 1. The one with four control points starts at a and ends at d. The two other points (b and c) control the direction in which the curve leaves a and enters d. It’s a bit difficult to explain but try around a bit with some random points and you’ll quickly understand how it works.
To use these with a line renderer, generate a number of points, let’s say 20, by first evaluating the function at t=0/19 then t=1/19, then t=2/19 and so on up to t=19/19. Set these points as the control points of the line renderer and you should get a nice, smooth curve.
I basically use 2 point Lerp in my drawing app:
Vector2 Bezier(Vector2 a, Vector2 b, float t) {
return Vector2.Lerp(a, b, t);
}
