- Home /
how to use t variable in the bazier curve
hello.
i'm doing a practice with bazier curve, by folow a tutorial i was able to get a cruved linerenderer, but what i want to do is to move an object by a curve, and here's the point.. how to do it, is fine to just put Time.deltatime as t variable?
this is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bezier : MonoBehaviour
{
[SerializeField]
private LineRenderer lineRenderer;
[SerializeField]
private Transform point0,point1,point2;
private Vector3[] positions = new Vector3[50];
private int numOfPoints = 50;
// Use this for initialization
void Start ()
{
lineRenderer.positionCount = numOfPoints;
}
// Update is called once per frame
void Update ()
{
//DrawLinearCurve();
DrawQuadraticBazierCurve();
}
private void DrawQuadraticBazierCurve()
{
for (int i = 1; i <= numOfPoints; i++)
{
float t = i / (float) numOfPoints;
positions[ i - 1] = CalculateQuadraticBezierPoint(t,point0.position,point1.position,point2.position);
}
lineRenderer.SetPositions(positions);
}
/*private Vector3 CalculateLinearBezierPoint (float t, Vector3 p0, Vector3 p1)
{
return p0 + t * (p1 - p0);
// P = P0 + t ( P1 - P0 )
}*/
private Vector3 CalculateQuadraticBezierPoint (float t, Vector3 p0, Vector3 p1, Vector3 p2)
{
float u = 1 - t;
float tt = t * t;
float uu = u * u;
Vector3 p = uu * p0;
p += 2 * u * t * p1;
p += tt * p2;
return p;
// B(t) = (1-t)2P0 + 2(1-t)tP1 + t2P2
}
}
Not an answer, but I do want to gently correct you- it's spelled with an e, "bezier" and you pronounce it like this: https://howjsay.com/search?word=bezier LCStark has a good answer, so good luck with your learning!
Answer by LCStark · Oct 01, 2018 at 03:40 PM
After you generate the Bezier curve (which you only have to do once - you don't need to do that every frame) you have an Vector3 array with positions of points on that curve. The object you want to move along that curve has to have access to those points and has to know how many points there are. Whether you do that by changing their accessors to public
or by provinding public methods to retrieve them is up to you.
All you need to do is make your object move towards each of these points, one by one. Have a field that stores the current point index and the position of your current target:
int currentPointIndex;
Vector3 currentTargetPosition;
In your object's
Update
function you can simply
MoveTowards
your current target position. If you reach that position, you simply increment the index and get the next point position. You'd also want to add a check if there are any more points, or if you've already gone through the entire curve:
transform.position = Vector3.MoveTowards(transform.position, currentTargetPosition, speed * Time.deltaTime);
if (transform.position == currentTargetPosition) {
currentPointIndex++;
if (currentPointIndex > numOfPoints - 1) {
Debug.Log("Done!");
}
}
actually I should do all of this in the calculate quadratic bazier methods?
Answer by Sylon87 · Oct 02, 2018 at 07:06 AM
Well i did it. But player doesn’t rotate with curve..
The player isn't supposed to, you have to code that manually. Here's what you should do: Take the old position, take the new position, and create a quaternion that represents a "look at" angle from the old to the new. Then, assign that rotation to the player. Here's one way you could do this:
transform.rotation = Quaternion.LookRotation( transform.position - oldPosition )
To get this oldPosition
variable, just assign the transform.position
to it before the move.
Actually using the direction from old to current position is not really exact. It may work for small enough steps. If you want to know the direction / tangent of a bezier curve, just use the first derivative. It gives you the tangent for the same given "t" and control point values.
private Vector3 CalculateQuadraticBezierTangent(float t, Vector3 p0, Vector3 p1, Vector3 p2)
{
float u = 1f - t;
return 3*(u*u*(p1 - p0) + 2*u*t*(p2 - p1) + t*t*(p3 - p2));
}
Your answer
Follow this Question
Related Questions
moving forward after bazier curve 1 Answer
Multiple Cars not working 1 Answer
witch approach for an car race curve 1 Answer
How to get lateral normals of a bezier curve 1 Answer
Text along a curve 2 Answers