Search Unity

Infinite line through two Vecrtor points.

Discussion in 'Scripting' started by Stakior, Oct 21, 2014.

  1. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    Hi Forum-People :D ,

    I need help with some scriptin'.

    So far I have a script that draws a line from point A to point B:

    using UnityEngine;
    using System.Collections;
    public class Linie : MonoBehaviour {
    void Update() {
    Debug.DrawLine(new Vector3(-5, -2, 7), new Vector3(3, 5, 10),Color.red);
    }
    }

    It works perfectly fine but I need the line to go through those two points and to be infinite on both sides :/

    Thanks for any help in advance :D

    Stakior
     
  2. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I'm not sure if it's possible, but then again I do not have that much experience.
    Do you have to have it infinite, or is it enough that it is shown all across the screen?

    If it is enough that the player/camera thinks it is infinite, you can calculate the line from the left side of the camera view edge to point A, and from B to the right camera/view edge.

    Else, well, just use really big numbers for the edges?
     
  3. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    :D I tried that, I can't get the line directly through the Points :/
    It would be better if the line would be infinite though .-.
     
  4. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Well, you could create an equation of the line from A to B, and then calculate it's position on the positive and negative ends.
    You know, y = m*x + n ?
    If that's not possible, then again, have it intercept with the camera view edges (which should have simple linear equations)
     
  5. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    Yeah I know that equation, but is it really the same like that ( y=m*x+n) in 3d space ? .-.
     
  6. DrSnake

    DrSnake

    Joined:
    Oct 17, 2014
    Posts:
    33
    Whats wrong with making the lines very long instead of infinite?

    Debug.DrawLine(new Vector3(-5000, -2000, 7000), new Vector3(3000, 5000, 10000),Color.red);
     
  7. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    Cuz it Needs to be infinite .-.
     
  8. DrSnake

    DrSnake

    Joined:
    Oct 17, 2014
    Posts:
    33
    How about:
    Code (CSharp):
    1. Debug.DrawLine(new Vector3(-5, -2, 7)* Mathf.Infinity, new Vector3(3, 5, 10) * Mathf.Infinity,Color.red);
    Does that work?
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Debug.DrawRay is infinite, though only in one direction. But you can use two! Just need to get the direction vector for first one then the other.
    Code (csharp):
    1.  
    2. Debug.DrawRay(point1, point2-point1);
    3. Debug.DrawRay(point1, point1-point2);
    4.  
     
  10. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    @ DrSnake No it doesn't work the line doesn't even apear anymore :/
     
  11. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I did not know DrawRay is infinite.
    If it really is, find a point between A and B, call it C.
    Then, like StarManta suggested, draw from C to B, and C to A, that way it goes both ways.
     
  12. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    hey bro.
    I didn't test this out, but I think this will work.
    you can multiply mathf.inifinity to the vector

    Code (CSharp):
    1. Vector2 v1 = new Vector2(2, 2);
    2. Vector2 v2 = new Vector2(-2, -2);
    3.  
    4. Debug.DrawLine(v1 * Mathf.Infinity, v2 * Mathf.Infinity, Color.red);
     
    Last edited: Oct 21, 2014
  13. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    I just get the same line, which starts at A and Ends at B
     
  14. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Oh.... huh. Sorry. I guess that was a bad assumption on my part.

    ......Why is Debug.DrawRay not infinite!? A Ray is infinite!
     
  15. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    @ StarManta Hey :D I gave you the coordintes with PointA (-5, -2, 7) and PointB (3, 5, 10) .

    Could you help me get These into your script from above ? :/ pliiiiieees