- Home /
How do you generate a line between objects that are only instantiated upon game Start ?
Hi Community, I believe there are many solutions out there addressing how to draw a line with your mouse or assigning existing objects to dependencies like generating a line, which is however not what I am looking for.
More importantly, how do you generate a line between objects that don't exist(prior to starting the game) ? I am currently doing a project using MapBox SDK, and my 'waypoints' (or objects) are only created upon starting the game.
Haven't been able to find any threads on this, thank you in advance :)
Sorry, this is not what I am looking for
Answer by MrRightclick · Jul 05, 2019 at 06:37 AM
Easiest way would be to assign a LineRenderer component to a GameObject (can be a new one or one of the objects you want to draw the line on), and then in script assign the positions of the LineRenderer component like so:
using UnityEngine;
public class DrawLineBetweenTwoObjects : MonoBehaviour
{
public GameObject objectOne; // The first object you instantiate
public GameObject objectTwo; // The second object you instantiate
public LineRenderer lineRend; // The linerenderer component, remember to assign this in the inspector!
void Start()
{
// Set the position count of the linerenderer to two
lineRend.positionCount = 2;
// Instantiate the two objects
objectOne = Instantiate(*yourFirstInstantiatedPrefabHere*, *yourPositionHere*, Quaternion.identity);
objectTwo = Instantiate(*yourSecondInstantiatedPrefabHere*, *yourPositionHere*, Quaternion.identity);
// Get the transform of the two objects
Transform first = objectOne.transform;
Transform second = objectTwo.transform;
DrawLineBetweenObjects(first, second);
}
void DrawLineBetweenObjects (Transform firstT, Transform secondT)
{
// Set the positions of the LineRenderer
lineRend.SetPosition(0, firstT.position);
lineRend.SetPosition(1, secondT.position);
}
}
Your answer
Follow this Question
Related Questions
LineRenderer use to predict a path? 0 Answers
How to plot a path according to a function in scene view 1 Answer
How to add LineRenderer positions based on an Array of GameObjects 2 Answers
Determine transform.position of location between two waypoints on bezier path 0 Answers
Need to find a point in a line using a percentage and a Vector3 list. 1 Answer