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;
}
}