Rotating cube so that is is perpendicular to mesh

The real world physical environment is a simple 4 walled room.

The scene is comprised of a scanned LiDAR mesh of these room walls.

I have an instantiated cube in my scene.

How would I orient or rotate this cube so that it is exactly perpendicular to one of these meshed walls?
Assuming that the Y-axis = ‘Up’ I would only need to rotate around the Y axis.
I know the mesh data includes normal vector data but have no idea how this might be used.
Any feedback appreciated!

Here is an example script I wrote some time ago.
The line you’re looking for is this one:

pointer.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);

6871118–801884–PlacePrefabOnMesh.cs (2.73 KB)

Thanks will give that a try. My challenge is that I am not using a Raycast to place the object on the mesh.
Instead the cube is instantiated in the middle of the physical room environment.
How do I obtain a normal in that case?

Raycasting is the only way to obtain a mesh normal at some particular point.

1 Like

You could potentially average out the normal-vectors for the vertices in the mesh (using a Job for performance).
But this would still be far more resource-intensive than using a raycast.
Meshes have their vertices in world-space, so the Up-vector for the object is useless.
Your best bet is thus a Raycast, as said above.

1 Like