Search Unity

Can Unity draw connections between sprites?

Discussion in '2D' started by davesili, May 22, 2019.

  1. davesili

    davesili

    Joined:
    Feb 27, 2019
    Posts:
    4
    Can Unity do this? I want the player to be able to place lots of circles on a plain background, then connect them as shown. Imagine that each step shown in the animated gif below is a snapshot along the way as the player drags the front of the wire to connect the circles. I'm imagining using a couple of resizable sprites to piece the wire together. Doable? Is there a better way? This is just an example of what I need to do. I really like the idea of writing this in C# and having it run everywhere, including in browsers, but I don't want to spend a lot of time on something that turns out to be impossible. :)
    Wiring.gif
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,090
    It's not whether Unity can do it, it's whether you can do it. Anything is possible; you program it to do what you want. If there isn't an automatic built-in way, then you do it yourself.

    --Eric
     
  3. davesili

    davesili

    Joined:
    Feb 27, 2019
    Posts:
    4
    Well that's good to hear. I would appreciate hints. I've spent quite a bit of time in tutorials and scouring the documentation, and I don't see it yet.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,090
    There are many many ways you could do it, so if you experiment with something and it works, go for it.

    --Eric
     
  5. davesili

    davesili

    Joined:
    Feb 27, 2019
    Posts:
    4
    I don't find anything helpful in your comment.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,090
    What about your first idea? Did you try that?

    --Eric
     
  7. davesili

    davesili

    Joined:
    Feb 27, 2019
    Posts:
    4
    I couldn't see where I could dynamically resize a sprite. Plus that seems like a really hokey approach. :)
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,090
    You can change the x/y scale of the transform.

    --Eric
     
  9. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,219
    research tile maps in unity, then when the finger or drag occurs you would choose a suitable tile image. It does take code however.
     
  10. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    2,465
    Streteching a quad or sprite is not a hokey approach, but I would personally use a line renderer component and set positions as you go. You can round the corners in the settings for it (i dont think you can get that wide of a radius without subdividing your corners into more positions though)

    You'll just need to decide on rules for how the line will be drawn as the player moves their cursor, because in order for it to always connect to the cursor (without the cursor perfectly moving along each axis like in the gif), you'll need to diverge the line onto the other axis a lot earlier.

    It could be as simple as this:
    • While the mouse is held on the first object
    • Create 4 points on the line renderer
    • Assign the first and last points to the first object and the cursor
    • Assign the second and third points to the X axis midpoint between the two, keeping the Y position of each
    • Once the mouse is within a distance of the second object, set the positions as usual but set the last position to the second object (gives the effect of snapping to it)
    You could get a lot fancier and create rules for whether the line starts vertical or horizontal, depending on whether the distance is more vertical or horizontal. It could split multiple times based on a distance-limit per segment, etc.

    https://docs.unity3d.com/Manual/class-LineRenderer.html
    Screen Shot 2019-05-23 at 11.10.13 AM.png
     
    Last edited: May 23, 2019