- Home /
Recalculate linerenderer when moving gameobjects
Hi!
I'm creating spheres that are linked between then using a linerenderer. What I need, and I don't know how to do it, is to "update" the linerenderer just when one of the spheres moves. This is my code:
[SerializeField] GameObject[] spheres;
[SerializeField] GameObject link;
private LineRenderer lineRenderer;
private void Awake () {
lineRenderer = link.GetComponent<LineRenderer>();
lineRenderer.positionCount = 2;
}
private void Start () {
CreateShape();
}
private void CreateShape ()
{
for (int i = 0; i < spheres.Length; i++)
{
for (int j = i + 1; j < spheres.Length; j++)
{
link.name = spheres[j].transform.name;
lineRenderer.SetPosition(0, spheres[i].transform.position);
lineRenderer.SetPosition(1, spheres[j].transform.position);
Instantiate(link, Vector3.zero, Quaternion.identity, spheres[i].transform);
}
}
}
Answer by tormentoarmagedoom · May 14, 2019 at 10:25 AM
Hello.
Where is the problem? If you get to correctly create the line, you only need to update the points.
Every time a sphere moves, you need to use lineRenderer.SetPosition again with the neew positions. As you do it refering directly the sphere , just do this again in update, or every time a sphere is moving
lineRenderer.SetPosition(0, spheres[i].transform.position);
lineRenderer.SetPosition(1, spheres[j].transform.position);
Bye!
Hi @tormentoarmagedoom ! Thanks for your answer, but I've already tried that and it doesn't work (I think because I'm instantiating the linerenderer)
are you sure you are trying to change the position to the corect linerenderer? i mean, $$anonymous$$eybe you continue instantiating things and linrenderer variable is now another thing or something like that...
And maybe is stupid question, and i dont know exactly what effect you pretend to achieve, but, did you tried with trail renderer?
And as last chance, watch this links:
https://forum.unity.com/threads/change-start-position-of-line-renderer.448059/
Consider using SetPositions (with S)
https://docs.unity3d.com/ScriptReference/LineRenderer.SetPositions.html
I had the same problem some years ago, but i rememebr i finally get it and was not difficult. I rememebr the fisrt things i tried didnt work, but finnally get it (without strange thigs)
Good luck! :D
Bye!
Your answer
Follow this Question
Related Questions
How to plot a path according to a function in scene view 1 Answer
What's the best way to draw a 2D line WITHOUT using LineRenderer? 1 Answer
move multiple 2d items in a circle around a fixed point 2 Answers
Neon / Glow effect on lines in 2d 1 Answer
How can I move an object to a clicked objects (center of X,Z) coordinates (board game)? 0 Answers