Search Unity

Interactive Panel.

Discussion in 'Getting Started' started by Garcia-Rojas, Sep 8, 2015.

  1. Garcia-Rojas

    Garcia-Rojas

    Joined:
    Sep 1, 2015
    Posts:
    16
    Hello everyone!

    I'm just going to explain what I'm looking for so I don't make this thread too long.
    I want to make a interactive panel that draw a line between 2 ponits, the first click on the panel with the mouse and the second one in a specifc point I decided before. I whould like to do this in a Canvas Image but I'm not sure how to start it.

    I post a simply image so everyone can understand what im trying to do. Thanks everyone in advance :).
    PopoImage.png
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    That's not going to be super easy; Unity doesn't have much in the way of line-drawing primitives, even in the new UI.

    I'd probably use a texture of a line, and just stretch & rotate this as needed to fit the two points.

    You could also use something like Vectrosity, though getting this to layer properly with your other UI elements might be challenging.

    Finally, see this blog post... I haven't tried it, but it might do what you need.
     
  3. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    That sounds hacky, but that's actually a pretty common approach to doing lasers in games. For lines in the scene you can also use a Line Renderer component.
     
  4. Garcia-Rojas

    Garcia-Rojas

    Joined:
    Sep 1, 2015
    Posts:
    16
    Alright, I decided to do it without the canvas but I found another problem with the way I'm doing it.

    I made 2 game objects InitialPoint and FinalPoint. Having a Quad faced to the camera beeing a Mask where a Ray can hit and set the InitialPoint when I click the mouse and if I keep pressing it, I move FPoint while is dragged. Is perfectly working when I separate the scripts but if I try to do it on the same (script) the InitialPoint is moving like if was the FPoint while FPoint is smoking a cigarrette.
    I know, is working in the other way but why isnt working on the same script?;

    Code (CSharp):
    1. public class LineDrawing : MonoBehaviour {
    2.  
    3.     public Transform fPoint;
    4.  
    5.     float camRayLength = 200f;
    6.     int pointMask;
    7.  
    8.     void Start () {
    9.         pointMask = LayerMask.GetMask ("PointMask");
    10. fPoint = GetComponent<Transform> ();
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.         if (Input.GetMouseButtonDown (0)) {
    17.             InitialPoint ();
    18.         }
    19.         if (Input.GetMouseButton (0)) {
    20.             DrawLine();
    21.         }
    22.     }
    23.     void InitialPoint ()
    24.     {
    25.  
    26.             Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    27.  
    28.             RaycastHit pointHit;
    29.  
    30.             if (Physics.Raycast (camRay, out pointHit, camRayLength, pointMask)) {
    31.    
    32.                 Vector3 playerToMouse = pointHit.point;
    33.  
    34.                 transform.position = playerToMouse;
    35.         }
    36.     }
    37.     void DrawLine()
    38.     {
    39.      
    40.  
    41.      
    42.         Ray camRay2 = Camera.main.ScreenPointToRay (Input.mousePosition);
    43.  
    44.         RaycastHit pointHit2;
    45.  
    46.         if (Physics.Raycast (camRay2, out pointHit2, camRayLength, pointMask))
    47.         {
    48.        
    49.             Vector3 playerToMouse2 = pointHit2.point;
    50.             fPoint.transform.position = playerToMouse2;
    51.  
    52.         }
    53.     }
    54. }
    55.  
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well in Start, you set fPoint = GetComponent<Transform>() (which is just a long-winded way of saying fPoint = transform). So fPoint and transform are the same thing. Now your InitialPoint() method is setting the transform position to playerToMouse, and your DrawLine method is setting it to playerToMouse2.

    It's not entirely clear to me what this script is intended to do, but I'm pretty sure that's not it!