Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Drag drop objects and detect collision

Discussion in 'Scripting' started by Pixitales, Mar 24, 2019.

  1. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    This is a top down 2D buiding game. Kinda like Sims builder. You buy furniture and decorate the interior of your house. I am a beginner with mostly copy/paste codes and made a few unique codes on my own.

    I have 2 tables and a character(player) in the scene. All of them has a rigidbody2D and a collider2D attach to them. I want to move the table to a different place in game. When I drag the table on top of another table or player, it just pushes the other table and player back because of physics and mass. All the furnitures needs to have the same rigidbody and collider.

    How do i prevent 2 or more of the same objects pushing each other when they collide with one of another? Are the other ways of detecting collisions without using a rigidbody?
     
    Last edited: Mar 24, 2019
  2. zioth

    zioth

    Joined:
    Jan 9, 2019
    Posts:
    111
    You could disable the collider while you're dragging, or turn on the "trigger" flag while dragging. With "trigger" on, you'll be able to detect collisions using OnTriggerEnter2D, OnTriggerExit2D and OnTriggerStay2D, but nothing will happen automatically. Everything will be controlled by your code.

    ("code" is both singular and plural, by the way. It's not "codes." Same goes for "furniture." Just some unasked for and probably unwanted extra information :) ).
     
  3. zioth

    zioth

    Joined:
    Jan 9, 2019
    Posts:
    111
    Oh, can't believe I didn't think of this right away: you can also use sorting layers to tell certain objects not to interact with other objects. Really, you probably never want furniture to interact with other furniture, so you can put all furniture in one layer, and, in project settings, tell that layer not to interact with itself.

    You'll then have to write your own code to make sure two pieces of furniture don't occupy the same spot.
     
  4. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    I need a collider or else i wont be able to use drag or triggerenter2d
     
  5. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Or disable physics. That kinda works, but it wont be able to tell there is collision so you can put a table on top of a table.

     
  6. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    I found a solution. I removed all the rigidbody2D(no more pushing) except from player and keep all the collider2D. I then use raycast2D to drag and drop objects and it works. Only problem is, I am going to need something else to replace OnTriggerEnter2D.... to using raycast to detect collision, but its a big leap forward. Thanks!

    Here is my 2D DragDrop with SnapToGrid and Raycast script. Only problem is... I haven't made any codes to detect collision yet and it selects an object automatically... its not finished
    Code (CSharp):
    1.  
    2.     void Update()
    3.     {
    4.         if (buttonManager.buildModeOn)
    5.         {
    6.             buildModeOn = true;
    7.  
    8.             if (Input.GetMouseButtonDown(0))
    9.             {
    10.                 RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    11.  
    12.                 if (hit.collider != null)
    13.                 {
    14.                     dragging = true;
    15.                     //Debug.Log("Target Position: " + hit.collider.gameObject.transform.position);
    16.                     //Debug.Log(hit.transform.name);
    17.  
    18.                     target = hit.collider.gameObject;
    19.                     screenPoint = Camera.main.WorldToScreenPoint(target.transform.position);
    20.                     offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    21.  
    22.                     interactButtons.gameObject.SetActive(true);
    23.                     var centerPoint = Camera.main.WorldToScreenPoint(target.transform.position);
    24.                     interactButtons.position = centerPoint;
    25.                 }
    26.             }
    27.  
    28.             if (Input.GetMouseButtonUp(0))
    29.             {
    30.                 dragging = false;
    31.  
    32.                 interactButtons.gameObject.SetActive(true);
    33.                 var centerPoint = Camera.main.WorldToScreenPoint(target.transform.position);
    34.                 interactButtons.position = centerPoint;
    35.             }
    36.  
    37.             if (dragging)
    38.             {
    39.                 Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    40.                 Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    41.                 target.transform.position = curPosition;
    42.  
    43.                 interactButtons.gameObject.SetActive(false);
    44.  
    45.                 //Snap to Grid
    46.                 target.transform.position = new Vector3(Mathf.RoundToInt(curPosition.x), Mathf.RoundToInt(curPosition.y), Mathf.RoundToInt(curPosition.z));
    47.             }
    48.         }
    49.         else if (interactButtons)
    50.         {
    51.             interactButtons.gameObject.SetActive(false);
    52.         }
    53.     }
    54.  
     
    Last edited: Mar 25, 2019