At this moment, I drag my game-object across the screen; however, I'm having a hard time figuring out how to have my game-object snap to pre-existing game-object in my scene. How do I have my game-object snapped to another object during run time once it is instantiated?
public class DragObject : MonoBehaviour
{
private Vector3 mOffset;
private float mZCoord;
void OnMouseDown()
{
mZCoord = Camera.main.WorldToScreenPoint(
gameObject.transform.position).z;
mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
}
private Vector3 GetMouseAsWorldPoint()
{
Vector3 mousePoint = Input.mousePosition;
mousePoint.z = mZCoord;
return Camera.main.ScreenToWorldPoint(mousePoint);
}
void OnMouseDrag()
{
transform.position = GetMouseAsWorldPoint() + mOffset;
}
}
LoadAssets class:
public class LoadAssets : MonoBehaviour
{
bool isCreated = false;
bool isCreatedDrillbit = false;
public void LoadBOP()
{
if (!isCreated)
{
GameObject instance = Instantiate(Resources.Load("BOP", typeof(GameObject))) as GameObject;
isCreated = true;
}
}
public void LoadDrillBit()
{
if (!isCreatedDrillbit)
{
GameObject instance = Instantiate(Resources.Load("Drillbit", typeof(GameObject))) as GameObject;
isCreatedDrillbit = true;
}
}
}