Hello, I’m relatively new to Unity and still trying to figure out all the odds and ends to it’s capabilities. I’m looking at selecting a mesh through mouse inputs, extrapolating the nearest vertices, and transforming them through dragging.
Is this kind of thing possible in Unity, and if so any suggestions on how I might go about it?
I’ve gotten about as far as finding the world point at which one has clicked by using the mouse x/y, and taking the z value by subtracting the object’s Y value from the camera’s Y (top down view, I swap the Z value I extrapolate the mouse y values for my raycast position). I then raycast from that point directly down, but I cant seem to find a way to get the vertex data from that point.
I seem to remember having read a function somewhere about get nearest vertex (or surrounding vertices), but I cant find it again.
Any help or suggestions would be greatly appreciated.
First of all, the mesh you want to manipulate will need a mesh collider with the exact same mesh in it. If it doesn’t, Physics.Raycast won’t find it.
After doing the raycast, the RaycastHit object (an optional parameter of Physics.Raycast) will contain lots of information (if it actually hit). This data includes the exact hit point and the triangle index of the triangle that was hit. This triangle index can then be used to modify the mesh.
Right, I’ve been using the raycast hit parameter, and using it elsewhere to determine heights of various objects. I guess I’m unsure as to how the triangle index works. I’ll look into that and see if I cant figure it out.
The triangle index was definatly the way to go. I’ve been able manipulate the triangles verticies of the raycastHit; however, I’m noticing some tearing. Are the triangles linked directly to the verticies, or do they have their own reference? The latter wouldnt really make sense to me, but it’s the only thing I can think of that would cause this.
What I’m doing is taking the triangle I hit, transforming the verticies to world co-ordinates, manipulating them in world co-ordinates, and then transforming them back into local co-ordinates. Then I re-assign the verticies to the mesh and the system handles the rest.
The triangle hit get’s adjusted properly all of the time; my problem lies with it’s neighbors. Some of the time, one of the vertices shared will not adjust for an adjacent triangle, causing the tear. It seems to occur only when the adjacent triangle shares two of it’s three verticies with the one being adjusted; however, not every time a triangle shares two verticies does it occur.
Any help/suggestions would be greatly appreciated.