Constrained direction perpendicular to a specific Plane or Mesh

I can successfully constrain the drag movement of a placed object along World Z or World X which is determined at application launch.
Very simply in an Update loop ondrag detect (pseudocode)

placedObject.transform.position.z = <my hit Pose z position>

However I need instead to constrain the direction so that it is the same as the normal x or normal Z of a specific Plane or Mesh.
In other words the constrained direction I really want is perpendicular to a specific Plane or Mesh.

How could I accomplish this?

You’ll want to determine the new location for your object in the local space for the plane. You can then use this local position and the planes localToWorldMatrix to find the worldspace-position and -rotation for your object.

If you don’t want to do any math: adding your GameObject as a child of the plane and using transform.localposition and .localrotation will also do the trick. Unity does the work for you.

1 Like