0
\$\begingroup\$

so I have some walls which are cubes stretched along a direction.and I have some windows and doors that are suppose to snap to a wall and don't let go until the desired location is out of the snapping range.and in the meantime I want the Objects to move along the wall but never detach from it.

if I wasn't clear enough let me know.

\$\endgroup\$
2
  • \$\begingroup\$ Can you describe better the scenario? Is it a 2D game? What engine/framework/language are you using? \$\endgroup\$
    – liggiorgio
    Commented Jun 15, 2016 at 12:16
  • \$\begingroup\$ It's 3D game . and as I tagged the question I'm using unity 3d and c#. \$\endgroup\$
    – Henjin
    Commented Jun 16, 2016 at 3:54

2 Answers 2

1
\$\begingroup\$

I have dabbled in some dragging and snapping behavior before. This script allows you to drag a cube, changes its color when its near the object to snap to and then snaps it to the right axis when you release the mouse. Sadly it has no code for dragging the object while its snapped to the wall but it might be a good starting point. Here is the code :

using UnityEngine;
using System.Collections;

public class Snap : MonoBehaviour {

  public  string partnerTag;
  public  float closeVPDist = 0.05f;
  public float farVPDist = 1;
  public  float moveSpeed = 40.0f;
  public  float rotateSpeed = 90.0f;

  private Vector3 screenPoint;
  private Vector3 offset;
  private bool isSnaped;
    Color color = new Color(1, 0, 0);

    float dist = Mathf.Infinity;
    Color normalColor;
    GameObject partnerGO;
    // Use this for initialization
    void Start () {
        normalColor = GetComponent<Renderer>().material.color;
        partnerGO = GameObject.FindGameObjectWithTag(partnerTag);
    }
    void OnMouseDown()
    {
        screenPoint = Camera.main.WorldToScreenPoint(transform.position);
        offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
        Cursor.visible = false;
    }
    void OnMouseDrag()
    {
        //transform.SetParent(null);
        Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
        Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
        transform.position = curPosition;
        Vector3 partnerPos = Camera.main.WorldToViewportPoint(partnerGO.transform.position);
        Vector3 myPos = Camera.main.WorldToViewportPoint(transform.position);
        dist = Vector2.Distance(partnerPos, myPos);
        GetComponent<Renderer>().material.color = (dist < closeVPDist) ? color : normalColor;
    }
    void OnMouseUp()
    {
        Cursor.visible = true;
        if (dist < closeVPDist)
        {
            transform.SetParent(partnerGO.transform);
            StartCoroutine(InstallPart());
            isSnaped = true;
        }
        if( dist > farVPDist)
        {
          //  transform.SetParent(null);
        }
    }
IEnumerator InstallPart()
    {
        while (transform.localPosition != Vector3.right || transform.localRotation != Quaternion.identity)
        {
            transform.localPosition = Vector3.MoveTowards(transform.localPosition, Vector3.right, Time.deltaTime * moveSpeed);
            transform.localRotation = Quaternion.RotateTowards(transform.localRotation, Quaternion.identity, Time.deltaTime * rotateSpeed);
            yield return new WaitForEndOfFrame();
        }
    }
}
\$\endgroup\$
-1
\$\begingroup\$

As far as I know Unity has only has snapping to grid by holding down Ctrl (Cmd on mac).

Unity has a tutorial for it here

\$\endgroup\$
2
  • \$\begingroup\$ thank you but I want to do it in runtime so I don't think that will be helpfull \$\endgroup\$
    – Henjin
    Commented Jun 16, 2016 at 4:00
  • \$\begingroup\$ This isn't even true in the editor. There's vertex snapping, surface snapping, and unit snapping. \$\endgroup\$
    – arkon
    Commented Feb 17, 2019 at 10:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .