Hello!

I'm trying to project an apple on a cube, in editor mode. Everything is simple - I take the object to the "safe" distance from the surface of the cube (red path), then I make a projection on the plane of the cube (blue path).

Trajectories

Next, I try to position the object according to the received coordinates.

Positioning

It turns out that the apple was positioned exactly in its local center, and came in half in a cube.

Question : how can one calculate the displacement for an apple so that it lies exactly before touching the surface?

As I understand it, Collision is responsible for this, but for a projection on any one you need a raycast. The only thing that comes to mind is a solution to the forehead - to move the object from the end of the red path to the point of reycast and look at the collisions, but, IMHO, this is kind of stupid, all the more you have to calculate the changes in thousandths of the apple position ...

In general, I know the Bounds of the apple mesh, but I’m not going to let my mind know how to calculate something using it, because the object can be rotated, and it’s unclear what axis to shift it, the reycast can successfully nail the apple as on top of a cube, as well as on the side, exactly like in any other places of any other mesh.

The code for this snippet is:

RaycastHit hitInfo = new RaycastHit(); Handles.color=new Color(1f, 0f, 0f); result += rotation * new Vector3(0, 0, 1f); Handles.DrawLine(position, result); Ray ray = new Ray(result, rotation * new Vector3(0, 0, -1)); // генерируем луч в сторону поверхности if (Physics.Raycast(ray, out hitInfo)) { Handles.color=new Color(0f, 0f, 1f); Handles.DotCap(0, hitInfo.point, rotation, 0.02f); Handles.DrawLine(result, hitInfo.point); result = hitInfo.point; } else { result -= rotation * new Vector3(0, 0, 1); } 

    1 answer 1

    I can offer the following options

    1. Use the known value of the "height" of an apple, this distance from its center to the lowest point, after which it should not be raised along the blue beam to this distance.

    2. Use the capability to find out if the objects intersect and use the half-division method from the initial point to the point where you already put it.

    3. Add gravity and the apple will fall :)