Search Unity

Question Constrained direction perpendicular to a specific Plane or Mesh

Discussion in 'AR' started by eco_bach, Feb 14, 2021.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    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)
    Code (CSharp):
    1. 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?
     
  2. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    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.
     
    eco_bach likes this.