- Home /
Draw a line from gameobject at end of the array to the gameobject in the start of the array
Hi all,
I have got line renderer drawing a line through all game objects in an array. However, I would like to draw the line renderer from the game object at the end of the array back to the game object at the start of the array. All game objects that I am drawing a line through are instantiated objects. This code below draws a line renderer from game object at start of array to the game object stored at the end of the array. Any help is appreciated.
What I am doing so far:
public void MeasureArea()
{
GameObject[] points = GameObject.FindGameObjectsWithTag("MeasuringPoint");
lrA = GetComponent<LineRenderer>();
lrA.material = new Material(Shader.Find("Transparent/Diffuse"));
lrA.startWidth = 0.1f;
lrA.endWidth = 0.1f;
pointLine = new Vector3[points.Length];
int n = points.Length;
for (int i = 0; i < n - 1; i++)
{
pointLine[i] = points[i % n].transform.position;
Debug.Log(points.Length);
}
lrA.numPositions = pointLine.Length;
lrA.SetPositions(pointLine);
Answer by HarshadK · Jun 02, 2017 at 11:40 AM
You need to pass your first gameobject as the last item in your array.
public void MeasureArea()
{
GameObject[] points = GameObject.FindGameObjectsWithTag("MeasuringPoint");
lrA = GetComponent<LineRenderer>();
lrA.material = new Material(Shader.Find("Transparent/Diffuse"));
lrA.startWidth = 0.1f;
lrA.endWidth = 0.1f;
int n = points.Length + 1;
pointLine = new Vector3[n];
for (int i = 0; i < n - 1; i++)
{
pointLine[i] = points[i].transform.position;
Debug.Log(points.Length);
}
// Just push first element at the last position.
pointLine[n-1] = points[0];
lrA.numPositions = n;
lrA.SetPositions(pointLine);
}
Thanks for input, however when I instantiate 4 points (game objects) only 3 points connect as required but the 4th point is all on its own with no lines. :S
@Harshad$$anonymous$$ This worked however I have an extra game object that isn't connected with the lines. e.g I placed 4 instantiated objects in the scene, but only lines are drawn from 3 points making a triangle. I am looking to draw lines between all game objects into the array, when at end of array it will draw a line to the gameobject at the start of the array. This was good but as mentioned above it left out a game object to form a triangle ins$$anonymous$$d of a square.
I made a change in code pointLine[n-1] = points[0]; to pointLine[n - 1] = pointLine[0]
as it gave a compiler error " Cannot implicitly convert type 'UnityEngine.GameObject' to UnityEngine.Vector3'.
Change the line from my code:
pointLine[n-1] = points[0];
to
pointLine[n-1] = points[0].transform.position;
@Harshad$$anonymous$$ Thanks again for revisiting. I have made the changes as suggested, however I still get the same behaviour. When 4 points are instantiated the line only goes through 3 of the points to form a complete triangle leaving the fourth point out.
Your answer
Follow this Question
Related Questions
Missiles move to already destroy objects [Problem][Update] 4 Answers
How to randomly select multiple gameobjects from array? 1 Answer
How do I change gameObject values in an array all at once? 1 Answer
Wrong object always Instantiated 1 Answer
How to make an array list set to false gradually instead of instantly? 2 Answers