title: how to snap two objects in runtime in unity?
body: <p><a href="https://i.stack.imgur.com/qoWwl.png" rel="nofollow noreferrer">this is the 3d model i wanted to connect another model like this to its silver connectors on top side  and also another model to right side(so do help me to snap it)</a>I want to know how to snap two 3D objects  together in runtime. i.e during "play" the user must be able to dragup, down, left, right to snap one object with the other object .for example like "lego", ie. one 3D object should snap to another 3D object. How would I achieve this?</p>
  
  <p>This is the code I use for dragging:</p>
  
  <pre><code>using System.Collections;
  
  using UnityEngine;
  
  public class drag : MonoBehaviour {
  
      Vector3 dist;
      float posX;
      float PosY;
      void OnMouseDown()
      {
          dist = Camera.main.WorldToScreenPoint(transform.position);
          posX = Input.mousePosition.x - dist.x;
          PosY = Input.mousePosition.y - dist.y;
      }
      void OnMouseDrag()
      {
          Vector3 curPos = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - PosY, dist.z);
          Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
          transform.position = worldPos;
      }
  }
  </code></pre>