Search Unity

How to calculate a position on a line renderer?

Discussion in 'Scripting' started by HashbangGames, May 7, 2012.

  1. HashbangGames

    HashbangGames

    Joined:
    May 7, 2011
    Posts:
    40
    Hey Peepz,

    I was wondering if someone could help me figure out how to calculate a position on a line renderer based on the position of another object.

    If you will, examine the below image. What i am trying to accomplish, is the following.

    Setup:
    1. I have a line draw from point A to point B.
    2. Ignore point C for now.
    3. I have a Box Collider around the LineRenderer.
    3. I have Point D which will be moving in and out of a collider at various positions on the lines Z axis

    What i need to do is calculate what the point "C" will be on the line. So that I can draw a new line from point C to point D.

    Also keep in mind that Point B Will be also moving along the X Y axis but will always have the same Z ( distance ) from point A.

    The variables that I have to work with are
    1. Point A position
    2. Point B position
    3. Point D position



    Could someone help me figure out this calculation, I have no clue how to do it.

    Thanks a bunch!
     
  2. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    I'm not sure what you mean bu a box collider around the line renderer, but collision has a Vector3 return value that you can use

    Code (csharp):
    1. void OnCollisionEnter(Collision col)
    2.     {
    3.         Vector3 test = col.contacts[0].point;
    4.     }
    Hope this helps
     
  3. HashbangGames

    HashbangGames

    Joined:
    May 7, 2011
    Posts:
    40
    Thanks chubbspet, but that is not the answer i was looking for. I already know how to get the position of the "D" point. Yes I too am getting that position from the OnTriggerEnter(Collider o){ } What I am looking for is the calculation to get the specific point on the line renderer so that i can draw a line as shown in the diagram.

    Take note of Point C on the diagram. Image if you will that "Point C" does not exist in the diagram.. and you had to draw a line from the line between point A and Point B .. how do you get Point C so that you can draw a straight line between the line and point D ?

    That is what the question is. How do i get Point C's position.
     
  4. jgb143

    jgb143

    Joined:
    Jun 8, 2010
    Posts:
    132
    If I understand what you're asking, what I would do is remove the collider around the line render and replace it with a linecast. Then create a big skinny box collider around point D that's almost like a plane. Then simply find where the line hits the plane. Unless you have some other reason for setting it in the way you describe.

    Otherwise, there might be some slick way to calculate position C using vector math, but I'd need a better a grasp on what you're trying to do.
     
    Last edited: May 7, 2012
  5. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Yeah, I'm also a bit confused, because based on your info the point c can be anywhere on AB. Are you maybe saying that AB will always be square with CD? If that si the case, trig is your answer, please let us know!
     
    Last edited: May 7, 2012
  6. jgb143

    jgb143

    Joined:
    Jun 8, 2010
    Posts:
    132
    Here's how I interpret it: Given points A, B, and D; find point C on line AB where C.x = D.x
    Then draw a line between C and D.

    Is this correct? If so, then should be able to use math to get the point.
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
  8. HashbangGames

    HashbangGames

    Joined:
    May 7, 2011
    Posts:
    40
    almost. but it would be "where C.z = D.z" We always what the z to be the same distance from the A point or a parallel A.
     
  9. HashbangGames

    HashbangGames

    Joined:
    May 7, 2011
    Posts:
    40

    This looks interesting, and definitely a usable solution, if i can figure out how to apply their pseudo code examples.
    I feel as though I'm getting closer to the best solution.

    Thanks! this gives me some direction. Once i find the best solution, i will post back with my findings.

    To everyone, More ideas are welcome
     
  10. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    That's not pseudocode, it's C++.
     
  11. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878