- Home /

# How To Make Quadratic Bezier Curve Through 3 Points

My code:

```
private Vector3 GetPointInPath(Vector3 playerPos, Vector3 netPosTop, Vector3 aimPos, float t)
{
float r = 1f - t;
return (r * r * playerPos) + (2f * r * t * netPosTop) + (t * t * aimPos);
}
```

Any idea how to make Bezier Curve go through netPosTop, instead of being controlled by it?

**Answer** by Bunny83
·
Feb 18 at 03:52 PM

You just can't because there are infinitely many bezier curves that will go through those 3 points. Keep in mind that if you have point A, B and C and your curve should start at A and end at C and go through B, the curve could start and end at various angles and stil go through B. So there's not a single solution for this problem. Have a look at this interactive Desmos drawing. I placed the middle control point at a position so the curve goes through the point B I have defined. Just try moving the middle control point around and you find infinitely many positions which all make your curve pass through point B.

Of course you could specify some constraints and extra conditions. However calculating the position of the middle conrol point is not that trivial depending on your extra conditions. For example if you want your thrid point being at t == 0.5 you can calculate the position of the middle control point by:

```
B = 2*yourPointOnTheCurve − 0.5f*A−0.5*C;
```

You simply get that equation by plugging in 0.5 into the bezier equation and solving for B.

```
p(t) = (1-t)² * A + 2f *(1-t) * t * B + t * t * C
// plug in 0.5 and define that p(0.5) == yourPointOnTheCurve
yourPointOnTheCurve = 0.25 * A + 0.5 * B + 0.25 * C
// solve for B
yourPointOnTheCurve - 0.25 * A - 0.25 * C = 0.5 * B
2*yourPointOnTheCurve - 0.5 * A - 0.5 * C = B
```

And there you have your answer. Of course if the point you want your curve to go through is extremely close to the first or last point you will get very weird curves since, remember, that point is at t==0.5. So the curve is bend too much. Here's an example

*edit*

Based on my comments below you can create some helper methods like this:

```
// untested code
public static Vector2 CalculateMiddlePoint(Vector2 aStart, Vector2 aEnd, float aTime, Vector2 aPoint)
{
float t = aTime;
float rt = 1f-t;
return 0.5f*(aPoint - rt*rt*aStart - t*t*aEnd) / (t*rt);
}
public static float CalculateMiddleTime(Vector2 aStart, Vector2 aEnd, Vector2 aPoint)
{
float a = Vector2.Distance(aPoint, aStart);
float b = Vector2.Distance(aPoint, aStart);
return a / (a+b);
}
public static Vector2 CalculateBestMiddlePoint(Vector2 aStart, Vector2 aEnd, Vector2 aPoint)
{
return CalculateMiddlePoint(aStart, aEnd, CalculateMiddleTime(aStart, aEnd, aPoint), aPoint);
}
```

Using `CalculateBestMiddlePoint`

and passing your two endpoints as the first and second argument and your third point as last parameter, this method should calculate the "best fitting" middle control point for your curve.

I just had some time and created this desmos example where you can drag around your 3 points and define the value "k" (slider on the left) to define where that third point should be on the curve. It will automatically calculate the 3rd control point. Now you can play around to see what the result will look like for certain positions and values for "k". Note I had to use "k" because "t" is already used internally by desmos.

*edit*

I just had the idea to calculate a "reasonable" k value we could simply use the distances between our 3 given points. The result looks like this. I will edit my answer and include that in my answer.

I was going to post one that I made but it's literally the same thing. I just never refreshed this page. Oops!

Oh, and thank-you, by the way.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

Rotating Bezier curves. 0 Answers

How to make game objects follow a path in order in Unity along a bezier curve 0 Answers