how do i make bezier curve handles dynamic?
first, I don't understand this bezier curve stuff that i am using too much, so if you can show me an easier aproach suitable to my situation, that would be very awesome.
I was out fishing for a tutorial on bezier curves and I eventually found one and I ended up with this script:
public LineRenderer line;
public Transform start;
public Transform end;
public Transform handle1;
public Transform handle2;
private float c;
private Vector3 h1;
private Vector3 h2;
private Vector3 q0;
private Vector3 q1;
void Start () {
q0 = CalculateBezierPoint(c, start.position, handle1.position, handle2.position, end.position);
line.SetVertexCount(100);
c = 0.0f; //for first curve c is set to 0
for (int i = 0; i < 99; i++)
{
c += 0.01f;
q1 = CalculateBezierPoint(c, start.position, handle1.position, handle2.position, end.position);
if (i == 0) line.SetPosition(i, q0);
line.SetPosition(i + 1, q1);
q0 = q1;
}
}
public Vector3 CalculateBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3) {
float u, uu, uuu, tt, ttt;
Vector3 p;
u = 1 - t;
uu = u * u;
uuu = uu * u;
tt = t * t;
ttt = tt * t;
p = uuu * p0;
p += 3 * uu * t * p1;
p += 3 * u * tt * p2;
p += ttt * p3;
return p;
}
As you might be able to see, this aproach is using 2 handles to calculate the bezier points. (I really don't know if I need that much I only want to have one single curve in my line. This is the part where I think it could be done more easily)
I need to have the handle(s) dynamic. so that the start and end object can move around in the scene and would still have the line connecting them. I tryed around some hours, but I only got straight lines. Wich faced me to another problem that also needs to be addressed..
..My World isn't a plane, but a sphere: So I need this line to always be outside (around) this sphere.
It also might be necessary to make the LineRenderer Size (VertexCount) dynamic, So I won't run into later performance issues.
I feel like this is too much to ask for or that this isn't the right place. If I should put this into the forums please tell me so. :\
Answer by Statement · Oct 25, 2015 at 06:43 PM
I think you can do this without beziers. All you are doing is mapping a linerenderer around a sphere with Vector3.Slerp.
Example project
SphericalLines.cs
Snap Transforms will position the From and To transforms on the sphere. You may want to turn that off for your game. It's useful for playing around with handles in the editor though. It'll update the LineRenderer in the editor so you see what you do. If you don't want that, remove [ExecuteInEditMode]
.
Wow, this is (almost) perfect! Thank you so much!
Almost because I might run into problems later when the sphere gets a detailed terrain. But that is far away, so I shouldn't worry about that too much right now.
However there is a small problem. when I open your project, I don't get that line in red. In fact I can't change the color of it at all. It allways stays black.
Well, change the material to something else :)
I did that, and in your project the albedo color of the material already is set to be red. however it is always black. In scene and in game view.
Your answer
Follow this Question
Related Questions
Create a curve with the mouse at the apex of the curve ? 0 Answers
Making Raycast line visible 2 Answers
Alternative to linerenderer for Tower Defence path display 2 Answers
Drawn line twists and becomes invisible 0 Answers
Line Renderer 2d Follow the edge 0 Answers