1
\$\begingroup\$

In Unity I currently have a script that allows me to "grab" a vertex, move it, and then update the mesh accordingly. How would I grab a line (which is basically grabbing two vertices I think) and move it?

I have read the manual (the scripting reference on Unity's site) but I can't really seem to figure out how to go about doing it. I got a lot of help with the initial script that lets me grab and move a vertex but I, unfortunately, am in need of guidance on how to extend its functionality to do what I described above.

For reference here are how things currently are working. Linked it because it says my gif is too big. Below is the functionality I'd like to add (hover over for text for further explanation).

I want to be able to grab the line (or basically the 2 vertices at once) and move it similar to how I did with just the single vertex before.

So how would I go about doing this? Also for the sake of keeping my project stable, I'm using Unity 5.3.2f1 but converting new to old code shouldn't be that big of a deal (or so I hope). Also I'mn doing this all at runtime.

\$\endgroup\$
0
\$\begingroup\$

Well, this might not be the cleverest solution to your problem, but it might get job done:

You have the fuctionality to grab a vertex; great. Now create a Vertex struct, that will store a Vector3, called position. You could select a vertex by pressing left-shift and the right mouse button and add all the selected vertices to a list. Then, you could say the "G" key let's you move all the selected vertices by the mouse movement you are applying to the vertex you have selected now.

Alternatively, you could use this method, where you detect the edges of a mesh, and once you have those, you can just do Edge.v1 and Edge.v2 to obtain the vertices (It's a little harder to understand though)

\$\endgroup\$
0
\$\begingroup\$

You might have some luck by using RaycastHit.triangleIndex(there's an example of how to use it in Unity Documentation) which returns indices of triangle hit with raycast. Having 3 verties that were hit you'd just have to guess which two of those three is most relevant to your click.

\$\endgroup\$
1
  • \$\begingroup\$ the problem with triangle ray casts is that unless you are inside the triangle then you won't get a tolerant hit on the line. That is,the way a triangle ray cast works is that you do a test to see where the ray hits the plane, then you do 3 dot prod tests against this to test if they are within the points of the vector. If any are > 1 then you are outside. \$\endgroup\$ – ErnieDingo May 14 '18 at 3:42
0
\$\begingroup\$

The high level solution I suggest will have resource on the net for each piece, I will just give the high level solution I can think of off the top of my head. Im sure there are faster versions.

  1. For each line (point a and point B), you need to test the following.
  2. test that the line segment (A->B) is firstly not parallel to ray cast.
  3. Create a plane based on ray cast and the line segment, the plane can be defined by a point (point a) and 2 vectors, in this case and the perpendicular vector (cross) of line segment (a->b) and ray cast.
    1. Test the ray cast against the plane. this will give you a point on the plane (point c).
    2. Test that the point is in between A and B by doing a dot prod < 0 where A->B dot A->C and B->A dot B->C < 0

What this test does is, you need to get the point on the plane where your ray cast hits. You then test on that plane if the point is between the points on the plane that define your line segment.

The pieces of code for each test can be found on the net, but start simple, define the point on plane test, then move on to the test of between points.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.