- Home /
Curved Line Renderer
Hello all,
I am trying to create a set of random curved lines coming out of a stick of dynamite. I am doing this in Unity c#. I need these lines to come out of one place (a dynamite object) but they then need to be random and curved.
I have, using the line renderer, created something that makes a line shoot out from the dynamite in a random path, like so:
The problem is, I need the lines to be curved. More like a rope, rather than straight like it is. Here is the code I have at the moment for creating the line. How would I go about adding curves?
//Find the Dynamite gameObject
GameManager.Instance.go_Dynamite = GameObject.Find("Dynamite");
//Find the line renderer fuse
GameManager.Instance.lr_Fuse = GameManager.Instance.go_Dynamite.GetComponent<LineRenderer>();
//Amount of turns in the fuse
int Amount = 10;
//Vector3 array for all the turn positions
Vector3[] Positions = new Vector3[Amount];
for (int i = 0; i < Amount; i++)
{
//If it is the first one
if (i == 0)
{
Positions[i] = new Vector3(0, 0.8f, 0);
}
//If its the second one
else if (i == 1)
{
Positions[i] = new Vector3(0, 1.5f, 0);
}
else
{
Positions[i] = new Vector3(Random.Range(-2f, 2f), Random.Range(-2f, 2f), 0);
}
}
//Set the position count
GameManager.Instance.lr_Fuse.positionCount = Amount;
//Set the positions
GameManager.Instance.lr_Fuse.SetPositions(Positions);
Any help would be greatly appreciated!
Answer by lgarczyn · Dec 21, 2019 at 08:17 PM
Sure, it's pretty easy:
Start by declaring two public variables for easy control:
public float maxStartingVelocity = 1f;
public float maxAcceleration = 0.2f;
Then add this code to replace your loop
Vector3 velocity = Random.insideUnitSphere * maxStartingVelocity;
Vector3 lastPosition = Vector3.zero;
for (int i = 0; i < Amount; i++)
{
Positions[i] = lastPosition;
lastPosition += velocity;
velocity += Random.insideUnitSphere * maxAcceleration;
}
The higher maxStartingVelocity
is, the longer your line will be
The lower maxAcceleration
is, the straighter your curve will be.
More points will make the curve longer, but if you do that with lowering maxAcceleration and maxStartingVelocity, it will also make it look better.
If the number of points is random, you will also get longer and shorter lines.
If you vary line thickness randomly, it will also look better.
You can also force a starting direction by setting the velocity yourself.
You can make the curves even curvier and weird like this:
Vector3 velocity = Random.insideUnitSphere * maxStartingVelocity;
Vector3 force = Vector3.zero;
Vector3 lastPosition = Vector3.zero;
for (int i = 0; i < Amount; i++)
{
Positions[i] = lastPosition;
lastPosition += velocity;
velocity += force;
force = Random.insideUnitSphere * maxJerk;
}
Hello @ceandros I am not sure if this is something I am doing wrong with this but this isn't quite working for me. I am not sure if I am just messing up the numbers or just generally being an idiot or something (also likely xD) but the lines I am getting now aren't right.
So, my original image had the right kind of length/turns etc but they were obviously straight lines. Using the code/ideas you have provided, the end result I am getting is more like this.
Like I say, if I am just being an idiot or something, I would be happy with that xD but at the moment the lines are just a heck of a lot longer. Whilst typing this out I have just tried again with some even smaller numbers (0.02, 0.04 etc) and it is better but the lines are still going in one direction, like the pictures shown. How would I get them to come back on themselves? (and preferrably stay within the screen).
Thanks again for the help :)
If you want them to come back on themselves, increase the number of points (100-200 should be fine), decrease maxStartingVelocity, and maybe increase the maxAcceleration.
You can also add these lines at the end of the loop
velocity -= lastPosition * lineGravity;
velocity *= 1 - lineDrag;
gravity being a public variable with a value from 0.2f for a strong force to 0.01f for a weak force. That force will help the line go back to the middle.
drag being a public variable to prevent the line from going "too fast" away from the center, and having a value from 0f to something like 0.3f maximum.
Thank you! I am stilling working out the numbers but it does do what I wanted it to do... finally! xD Thank you soo much for the help :D
End result (at the moment) looks like this:
That is with variables set as:
Amount: 100 $$anonymous$$axStartingVelocity: 0.02 $$anonymous$$axAcceleration: 0.04 Line Gravity: 0.01 Line Drag: 0.01
So, thank you soo much for the help @ceandros :D
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
customize line renderer from middle 0 Answers
GUI text not showing in scene or game view 0 Answers
Scale fishing rod in one direction 2 Answers