Search Unity

[ Solved ]draw a line between 2 objects at a set distance ?

Discussion in 'Scripting' started by t4d, Sep 7, 2009.

  1. t4d

    t4d

    Joined:
    Mar 19, 2009
    Posts:
    7
    I hoping for some help to get the line render active
    when ingame objects are a set distance from each other ?

    sorry complete newbie here and need some direction ?
     
  2. matthewminer

    matthewminer

    Joined:
    Aug 29, 2005
    Posts:
    331
    If I understand correctly, you want a line to appear between two game objects only when they're a certain distance from each other? If that's the case, Vector3.Distance will tell you how far apart two objects are. Try something like the following:

    Code (csharp):
    1. var target : Transform;
    2. var minDistance : float = 10;
    3. var maxDistance : float = 15;
    4.  
    5. function Update () {
    6.     var line : LineRenderer = GetComponent(LineRenderer);
    7.     // Determine the distance between this game object and the target
    8.     var distance : float = Vector3.Distance(transform.position, target.position);
    9.    
    10.     // Enable the line renderer if it's between the min and max distance
    11.     line.enabled = distance > minDistance  distance < maxDistance;
    12.    
    13.     // If the line renderer is enabled, set its first and second positions
    14.     if (line.enabled) {
    15.         line.SetPosition(0, transform.position);
    16.         line.SetPosition(1, target.position);
    17.     }
    18. }
     
  3. t4d

    t4d

    Joined:
    Mar 19, 2009
    Posts:
    7
    Thank you SOOOO Muchm Very COOL !! :D

    if you need 3D content happy to help out any time 8)