I’m actually trying to make a similar control like Angry Bird. I want to make a bezier’s curve renderer in real time acording to input touch. And when input ends I want to shoot an arrow exactly in the same direction. Which it’s best way to make this? I’ve already have this mechanism almost working with a Line Renderer and Vector3.Lerp but I want to change it to a curve.
This code gives you a point on a bezier curve, given the 4 control points and a value between 0 and 1 (0 being the start of the curve, 1 the end).
public class Bezier {
public static Vector2 interpolate(Vector2 start, Vector2 end, Vector2 A, Vector2 B, float f) {
Vector2 a = Vector2.Lerp(start, A, f);
Vector2 b = Vector2.Lerp(A, B, f);
Vector2 c = Vector2.Lerp(B, end, f);
Vector2 d = Vector2.Lerp(a, b, f);
Vector2 e = Vector2.Lerp(b, c, f);
return Vector2.Lerp(d, e, f);
}
}
But for this, a bezier curve is not really applicable, since you only have two control points (the start position and a vector based of the start velocity). For this I would use a basic euler integration:
Vector2 startPos, startVel; //these would be set via your touch input
Vector2 gravity = new Vector2(0, -1); //change as needed
int steps = 10; //how many points
float distance = 10; //how far
Vector2[] curvePoints = new Vector2[steps];
curvePoints[0] = startPos;
Vector2 tmpVel = startVel;
float h = distance/steps;
for (int i = 1; i < steps; ++i) {
curvePoints _= curvePoints[i-1] + h * tmpVel;_
tmpVel += h * gravity; } The curvePoints array now holds the points that can be drawn either via a LineRenderer or any other way of visualisation.