- Home /
Draw a bezier curve and shoot a projectile in same direction
Hello!
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.

How about the opposite. Ins$$anonymous$$d of making your projectile follow a curve, calculate the curve based on the physics values used for the object. That would make it easier on impact, cause everything is already physically setup.
How can I calculate that? I'm intermediate skilled in C# but I don't know anything about advanced maths. What should I do?
Answer by Piflik · Jun 25, 2015 at 11:00 AM
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[i] = 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.
+1
For some reason i can't find the question to this example i've made (After 20 different specialized google searches i've given up...^^)
There's a unitypackage link at the bottom.
(I really should add the question link to those builds ^^)
I've finally make my script working with this, thank you guys :)
Answer by CasperK · Jun 25, 2015 at 12:39 PM
I Like to use a method like this, having it possible to poll on any part of the curve. Its based on y = gt^2/2 + vit.
     float gravity=-.91f;
     Vector2 PolCurve(Vector2 position,Vector2 speed,float time)
     { 
         return (new Vector2(position.x + speed.x*time,position.y + gravity * (Mathf.Pow(time,2f))/2f + speed.y * time));
     }
That way you can also have them moving along the line like some games have by using an offset on the time.
This could works too but I didn't test it, thank you anyway :)
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                