Why my pivot point is only taken on the first LineRenderer?
Hi everyvone,
I would to create different LineRenderers with a rotation for everyone. Each new LineRenderer is rotated around a random pivot point taken on the previous LineRenderer.
For example, I create the first LineRenderer named l0, then, I create the next LineRenderer l1 by "cloning" l0 (with Instantiate command), taking a random Vector3 point vec on l0 and rotating l1 around vec. Then, I create l2 in the same way: cloning l1, taking a new random point vec on l1 and rotating l2 around vec. (see the above picture)
But the result is not what I'm expected. Each vec pivot point is taken on the first LineRenderer l0, therefore the next LineRenderers will not be close to their previous LineRenderer.
Do you know why? (Bonus question: two other spheres is created but I don't know why)
Result
The code
public class DungeonBuilder : MonoBehaviour
{
void Start()
{
//Start Room
LineRenderer l0 = CreateStartRoom(); //Create a new LineRenderer in the space
//Next rooms
LineRenderer l1 = CreateRoom(l0, 0);
LineRenderer l2 = CreateRoom(l1, 1);
}
private LineRenderer CreateRoom(LineRenderer old, int i)
{
//Choose a color
var c = Color.white;
if (i == 0) c = Color.red;
if (i == 1) c = Color.blue;
if (i == 2) c = Color.green;
if (i == 3) c = Color.black;
//Creation of the LineRenderer
LineRenderer l = Instantiate(old);
l.material = new Material(Shader.Find("Sprites/Default"));
l.startWidth = 0.01f;
l.endWidth = 0.01f;
l.startColor = c;
l.endColor = l.startColor;
l.useWorldSpace = false; //For using the Transform component of LineRenderer
//Get a random vertice/point of the LineRenderer
Vector3 vec = old.GetPosition(Random.Range(0, old.positionCount));
//Rotation
l.gameObject.transform.RotateAround(vec, Vector3.up, 180);
//Just a visual indication to see the pivot point
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.GetComponent<MeshRenderer>().material.SetColor("_Color", c);
sphere.transform.localScale = new Vector3(-0.5f, -0.5f, -0.5f);
sphere.transform.position = vec;
return l;
}
}
Answer by streeetwalker · Oct 12, 2020 at 01:50 PM
It seems the problem is that your taking a point on the old LIneRenderer to use as the pivot for the new LIneRenderer, but because you've set up the LineRenderers as localSpace those points are local to the LineRenderers transform (i.e. referenced from 0,0,0 ).
They do not produce the rotation you want because the RotateAround is acting as though those are in world space.
So you need to first convert the pivot point you selected from local to world space. Off the top of my head you can do this with old.transform.TransformPoint( vec )
and use the result in your RotateAround call.
Your staring LineRenderer must have been set to world space, so it appears the 2nd one that you clone is rotated properly. But from thereafter you've set each new one to local space. Either that or your first one is located at 0,0,0 - in that case the points are the same as their world space equivalents.
You've got to pay for the bonus question - otherwise it's not a bonus! ;-) Jest aside, I think the problem will take care of itself once you handle the pivot point issue.
Answer by roroquessada · Oct 14, 2020 at 03:50 AM
Thank you so much! it works. I have to better understand these stories of spatialization ^^
Your answer
Follow this Question
Related Questions
Line Rendering for 2D Connect the Dots Game 1 Answer
Line renderer not working 1 Answer
LineRenderer in 2D not reusing positions 0 Answers