Draggable lines

Hello, I am trying to create something in unity, but I am not sure how to and not quite sure what to google, everything that I have googled brings up something slightly related but not enough.

Basically I am wanting to have multiple boxes in a scene, and lines that connects one box to another one. But I want to be able to grab the end of one of the lines and move it and attach it to one of the other boxes. When I am moving the line, I am wanting to to expand in length so that It can reach the other boxes, as the boxes will be at different distances from each other.

I am not looking for someone to give me the code directly, but If anyone possibly knows any links to something similar that I can read about, or have better ideas on what I could google, or just anything that could help me with this issue. I am not too familiar with unity, i have programmed in other languages, but not a lot to do with games.

Hi and welcome!

First and foremost: this is the norm. Unless you intend to reproduce something done by someone else, what you find is usually not 1:1 what you are looking for. So gamedevelopment, even softwaredevelopment in general, requires a good portion of being able to transfer knowledge or even partial knowledge to fit ones custom circumstances.

As a general piece of advice: divide and conquer.
When a problem is too hard or too complex to solve, break it down into simpler, smaller problems. Repeat this until you arrived at a problem you can either directly solve / reason about, or google more easily.

Edit: I accidentally posted before finishing my post, so there is the rest. Sorry.

What do we have here?
You need a line. What is a line? A sequence of points. So whether you use a linerenderer, or some custom visualization, you will always have some points between which you render a line.

You now want to be able to drag the last point. Assuming you re-render the line from the points each frame, that’s all done, really. So how to drag the last (or any) point of your line? Several solutions are possible again. You could write a function which checks for MouseDown and when that happens checks if the raycasted mouse position (or screen position) is close enough to one of your points, if so “grab it”. You may also have each point be a gameobject which feeds its position into the array of points each frame, so you now only need to handle dragging gameobjects which may be easier.

Something along those lines should be a viable approach.

1 Like

As Yoreki pointed out, that’s about as good as it gets.

Also as Yoreki pointed out, divide and conquer.

You need a line. Forget about EVERYTHING else.

Find three different ways to put a line onscreen and understand fully how they work.

For instance, here are some random ways to make a line, in steady order of complexity:

  • use a LineRenderer (you probably want this one, it works, and it’s built in and free)
  • buy Vectrosity in the asset store (it’s like LineRenderer Ultra Supreme Edition)
  • use a sprite, stretched (lotta math, but it might be advantageous in some cases)
  • use a series of small sprite segments, all laid end to end (has other benefits)
  • produce your own geometry dynamically (very powerful, very advanced)
  • use UI Extensions package from github (again, gotta integrate it, but it does a lot)
    — >> more?

ONLY once you understand getting at least a line onscreen, set it aside and start understanding how stateful touch input works, dragging stuff, the IDraggable interface stuff, doing it yourself, etc.

Finally you bring it all together. That’s software engineering.

1 Like

Hello, thank you both for the answers. I have done as you both suggested and started out with just creating one part. I now have a line that I can drag around, I done this using the line renderer. now i am going to focus on attaching it. :slight_smile:

1 Like