Reputation:
As the title suggests, the idea is to build meshes between one sphere and another, make the meshes transparent by disabling its mesh renderer or by setting alpha color to zero. At present, because everything works, I have to assign a new layer to all the meshes because they are not parallel on the floor but according to the direction they can be oblique. In this case the RayCast would not see the part of the mesh hidden by the plane. Using the layer on RayCast I get the collider on the entire surface of the mesh. But I wonder if there is no way to set the 4 Vector3 that I pass to create the mesh so that it is parallel to the plane. To find the 4 vertices of the mesh I used a script created to draw more parallel Gizmos lines to get a greater thickness. Once I have the 4 vertices it is easy to create a mesh. I also tried using linerenderer setting startWidth and endWidth to get a line with apparently parallel edges in Game View. It behaves in the same way. Furthermore the BakeMesh method of linerenderer is available only for very recent versions of Unity. This is an image of the result, I have enlarged the mesh so that its inclination is clearly visible
As you can see only the selected line, which is perfectly perpendicular in Game View, rests on the plane.
This is the code, I find the four vector3 to the left and to the right of the central position of the sphere and I store them in a list:
Vector3 p1 = sphere1.transform.position;
Vector3 p2 = sphere2.transform.position;
Vector3 scp1 = cam.WorldToScreenPoint(p1);
Vector3 scp2 = cam.WorldToScreenPoint(p2);
Vector3 v1 = (scp2 - scp1).normalized;
Vector3 n = Vector3.Cross(v1, Vector3.forward);
Vector3 newVert;
Vector3 o = 6f * n * 3 * (0f / 3 - 0.5f);
newVert = cam.ScreenToWorldPoint(scp1 + o);
listVerts.Add(newVert );
newVert = cam.ScreenToWorldPoint(scp2 + o);
listVerts.Add(newVert );
o = 6f * n * 3 * (1 - 0.5f);
newVert = cam.ScreenToWorldPoint(scp2 + o);
listVerts.Add(newVert );
newVert = cam.ScreenToWorldPoint(scp1 + o);
listVerts.Add(newVert );
Then I pass the list to a function that creates the mesh. What do I have to work on to ensure that the meshes are all resting on the floor? Is there a better way to get points parallel to the center of the sphere?
Upvotes: 1
Views: 388
Reputation: 20270
One way is to just overwrite the y
component of each vertex with the y component of the bottom of the sphere it's next to.
// I recall from your previous question that you have a formula to
// calculate the scale / radius directly but it's not in the question,
// so here's a way to estimate it:
float sphere1Radius = p1.transform.lossyScale.x / 2f;
float sphere2Radius = p2.transform.lossyScale.x / 2f
float sphere1BottomY = p1.y - sphere1Radius;
float sphere2BottomY = p2.y - sphere2Radius;
newVert = cam.ScreenToWorldPoint(scp1 + o);
newVert.y = sphere1BottomY
listVerts.Add(newVert );
newVert = cam.ScreenToWorldPoint(scp2 + o);
newVert.y = sphere2BottomY
listVerts.Add(newVert );
o = 6f * n * 3 * (1 - 0.5f);
newVert = cam.ScreenToWorldPoint(scp2 + o);
newVert.y = sphere2BottomY
listVerts.Add(newVert );
newVert = cam.ScreenToWorldPoint(scp1 + o);
newVert.y = sphere1BottomY
listVerts.Add(newVert );
Upvotes: 0