4

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)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?

This is the code I use for dragging:

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;
    }
}
Aishu
  • 51
  • 2
  • 2
  • 7
  • Depends on how you want to snap them together. Connect them at specific joints? Have them exactly at the same position? – Ian H. Aug 01 '17 at 11:13
  • Google for snapping logic? Try to figure out how to get objects to snap together (x and z threshold maybe) – rakagunarto Aug 01 '17 at 11:14
  • @lan H. i have edited please do see it. i want to connect another model to the top of its connector and also another model to right side of connector.please do help – Aishu Aug 01 '17 at 16:59
  • @bi0phaz3 yea thanku.but sorry i have surfed a lot for a week. din get anything worked – Aishu Aug 01 '17 at 17:00

1 Answers1

0

There are many many ways to accomplish this. Here is the first method I came up with. If we break down what might comprise a snap, we can muster up a script to attach to each snappable child:

1) Assign tag "parentblock" to the object you are dragging around.
2) Attach a trigger collider to both the parent object and the snappable child object.
3) When the the dragged object enters the collision area, snap it to the parent.
4) Store the offset from the parent to maintain its position once snapped.

bool snapped = false;
GameObject snapparent; // the gameobject this transform will be snapped to
Vector3 offset; // the offset of this object's position from the parent

Update()
{
    if (snapped == true)
    {
        //retain this objects position in relation to the parent
        transform.position = parent.transform.position + offset;
    }
}

void OnTriggerEnter(Collider col)
{
    if (col.tag == "parentblock")
    {
        snapped = true;
        snapparent = col.gameObject;
        offset = transform.position - snapparent.transform.position; //store relation to parent
    }
}

Keep in mind this will only snap child objects to 1 master parent object that you are dragging around. It should go without saying you may need to tweak this for it to perform specifically to your project like you want, but hopefully this gets you in the right direction. (FYI I have not tested this so am not guaranteeing it will work right out of the gate)

ryeMoss
  • 4,303
  • 1
  • 15
  • 34