Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Align lign renderer in a straight angle relative to a mesh

Discussion in 'Scripting' started by shura0123, May 28, 2018.

  1. shura0123

    shura0123

    Joined:
    Jun 26, 2017
    Posts:
    8
    What I am trying to figure out is how can I do an alighnment on a vertical/horizontal axis relative to a mesh.

    I use a raycast to position my points once 2 points have been set the line renderer renders between the 2 points with SetPosition and Quaternion.LookAt(hit.normal) to set the normal and the line is set to world space position. What I am trying to achieve is once the line renderer is set, with a button i want that line renderer to be set or rotated on a straight angle either vertically or horizontally relative to the mesh. (I already know how to setup the button. Anyone with helpfull advice would be great. thank you


    I have drawn a quick mockup example.png
     
  2. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    Not sure if that is what you want or if it even works but it looks legit.

    Code (CSharp):
    1.     Vector2 firstPoint = new Vector2(1,5);
    2.     Vector2 secondPoint = new Vector2(10,0);
    3.  
    4.     if(Mathf.Abs(a.x - b.x) < Mathf.Abs(a.y - b.y))
    5.     {
    6.         Vector2 straightPoint = new Vector2(a.x,b.y);
    7.     }
    8.     else
    9.     {
    10.         Vector2 straightPoint = new Vector2(b.x,a.y);
    11.     }
     
  3. shura0123

    shura0123

    Joined:
    Jun 26, 2017
    Posts:
    8
    Thank you for the reply.

    I forgot to mention that it is in 3d but i have tried a similar approach and I am not getting the desired result.
     
  4. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    I think y
    Code (CSharp):
    1. Vector3 line1;
    2. Vector3 line2;
    3.  
    4. Vector3 v =  line2 - line1;
    5. float dot = Vector3.Dot(transform.forward, v);
    6. line2 = transform.forward * dot;
    ou want to use the vector dot product. The dot product of a vector tells you how much one vector points in the direction of another vector. If one of the vectors is normalized, then you could use the dot product to get the point your looking for.

    As an example, let's say you have the second position of the line and you want get its position along the transforms forward normal. First subtract the first position of the line from the second, so that the second is centered around the origin, then get the dot product between the forward normal and the new second position. Then multiply the dot product by the forward transform to get the point.