Quantcast
Channel: Answers by "Fariel"
Viewing all articles
Browse latest Browse all 4

Answer by Fariel

0
0
Times like this I find it is important to take a step back and write out, verbally, what I'm trying to do. You have an array of objects you need to draw a line to. Since it appears that objects can only have one line renderer on them (don't quote me on this. I just tried it in the editor but I don't know if it works in code) we would probably do best to add the line renderers to the objects as they get close. So we need to add something when it gets close, remove it when it leaves. Sounds like trigger mechanics to me. Our trigger object, which would be the same as your attractor, would look something like this: void OnTriggerEnter (Collider objectEntering) { Debug.Log ("trigger entered"); //check to see if it has a line renderer already. If not, continue. if (!objectEntering.GetComponent ()) { //add a line renderer, a "line draw to here" component //and then tell the "line draw to here" component to draw to this object. objectEntering.gameObject.AddComponent (); objectEntering.gameObject.AddComponent (); objectEntering.SendMessage ("DrawLineToThis", transform); } } void OnTriggerExit (Collider objectLeaving) { Debug.Log ("object left"); //check to see if the object has a influence line component //if so, we need to remove both of the influence components. Destroy (objectLeaving.GetComponent ()); Destroy (objectLeaving.GetComponent ()); } While our new component, the InfluenceLine component, would look something like this: RequireComponent (typeof(LineRenderer))] public class InfluenceLine : MonoBehaviour { Transform storedTarget; LineRenderer lineRend; bool targetIsSet = false; void Start () { lineRend = GetComponent (); } void Update () { if (targetIsSet) { lineRend.SetPosition (0, transform.position); lineRend.SetPosition (1, storedTarget.position); } } public void DrawLineToThis (Transform target) { storedTarget = target; targetIsSet = true; } } Hopefully this helps push you in the right direction. :) Edit: Screenshot of it in action in the editor ![alt text][1] [1]: /storage/temp/40972-untitled.png

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images