- Home /
Question by
Barlium · Nov 14, 2018 at 01:14 PM ·
renderinglistlinerenderer
Multiple Lines in 3D using LineRenderer in one Script
Hi there,
so I want to draw lines between different GameObjects in 3D.!
I have two fbx-models with body parts. Those models are moving. The lines to be drawn should be between eg: LeftForeArm of the one Model, and the LeftForeArm of the other model. There needs to be lines between all the body components between model 1 and model 2.
What I know so far: I can only use one static LineRenderer in one script. I need to dynamically create a List with LineRenderers for all body parts - but I cant figure it out myself!
This is the initialization code snippet:
private void drawingLinesInitialization()
{
// Line Renderer Initialization
LineRenderer lineRendererLeftForeArm = gameObject.AddComponent<LineRenderer>();
//Line Renderer Colors / Materials
lineRendererLeftForeArm.material = new Material(Shader.Find("Sprites/Default"));
//Line Renderer Width
lineRendererLeftForeArm.widthMultiplier = 0.05f;
/*
* A simple 2 color gradient with a fixed alpha of 1.0f.
* ColorGradient retrieved from Unity example:
* https://docs.unity3d.com/ScriptReference/LineRenderer.SetPosition.html at the 14.11.2018
*
*/
float alpha = 1.0f;
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] { new GradientColorKey(c1, 0.0f), new GradientColorKey(c2, 1.0f) },
new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) }
);
lineRendererLeftForeArm.colorGradient = gradient;
}
This is the code I have so far for one comparison:
IEnumerator coroutineDrawingSystem()
{
while (true)
{
//Debug.Log("Coroutine Drawing System called");
//Getting Components:
LineRenderer lineRendererLeftForeArm = GetComponent<LineRenderer>();
//Checking if GameObjects are initialized and then drawing
if (cLeftForeArm != null && lLeftForeArm != null)
{
// Update position of the two vertex of the Line Renderer
lineRendererLeftForeArm.SetPosition(0, cLeftForeArm.transform.position);
lineRendererLeftForeArm.SetPosition(1, lLeftForeArm.transform.position);
} else
{
Debug.Log("Some Transforms of objects are not initialized!");
}
yield return new WaitForSeconds(timeBetweenLineRenderDrawing);
}
}
unbenannt.png
(20.0 kB)
Comment