I have the following code which is attempting to move an object along a path of 3 defined points using my interpretation (which is most likely the problem) of a Bezier curve. I am aiming longterm to change the EndPoint variables to the StartPoint variables to start a new curve from the end of this one.
I have solved the problem I had and this code now works for anyone who wants the same kind of effect.
var StartPointX: float = 0;
var StartPointY: float = 0;
var ControlPointX: float = 20;
var ControlPointY: float = 50;
var EndPointX : float = 50;
var EndPointY : float = 0;
var CurveX:float;
var CurveY: float;
var BezierTime: float = 0;
var mySphere: Transform;
function Update()
{
BezierTime = BezierTime + Time.deltaTime;
if (BezierTime >= 1)
{
BezierTime = 0;
}
CurveX = (((1-BezierTime)*(1-BezierTime)) * StartPointX) + (2 * BezierTime * (1 - BezierTime) * ControlPointX) + ((BezierTime * BezierTime) * EndPointX);
CurveY = (((1-BezierTime)*(1-BezierTime)) * StartPointY) + (2 * BezierTime * (1 - BezierTime) * ControlPointY) + ((BezierTime * BezierTime) * EndPointY);
transform.position = Vector3(CurveX, CurveY, 0);
}
<p>I hate to do this, but which variables control the speed of how fast the object moves along the curve? </p> <p>I assume it's Beizertime, but I've attempted to manipulate this variable, and it doesn't seem to slow the object down. </p>
– anon40666834I have edited to the above question to show a now working solution
– anon40480366So helpful!!! Is the 3D version simple?
– anon17683503CurveZ = (((1-BezierTime)*(1-BezierTime)) * StartPointZ) + (2 * BezierTime * (1 - BezierTime) * ControlPointZ) + ((BezierTime * BezierTime) * EndPointZ); << - add this and you'll have the 3D version. Of course, you'll have to declare the necessary variables for Z.
– zannghastThis is a great solution. But I have to ask, how do you control the speed variable? by default, the object is way too fast. Thanks again!
– anon40666834