2D Drag and Drop, but not when slot is already full
Hi,
First time asking for help, I'm very new to Unity but I've managed to create 90% of a game so far. I'm on the last puzzle and I can't get my head around this issue.
Basically, I have 35 objects and 3 slots to drop the objects on to. If there is already an object in a slot, it should be considered that the slot is busy and if the user tries to drop a second object onto an occupied slot, it should snap back to it's original location. The script I have deals with the drag and drop and the snapping etc, but I can't figure out how to stop the user dropping multiple items into the same slot!
Here's a screenshot, the code follows:
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragViles : MonoBehaviour
{
private bool isDragging;
bool isInSection1, isInSection2, isInSection3, isInShelfArea, section1busy, section2busy, section3busy;
float old_z;
bool section1Full = false;
public void OnMouseDown()
{
old_z = this.transform.position.z;
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, -1.671000f);
isDragging = true;
}
public void OnMouseUp()
{
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, old_z);
isDragging = false;
if (this.transform.position.x == GameObject.Find("position_1").GetComponent<BoxCollider2D>().transform.position.x && this.transform.position.y == GameObject.Find("position_1").GetComponent<BoxCollider2D>().transform.position.y)
{
section1Full = true;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "position_1")
{
isInSection1 = true;
isInSection2 = false;
isInSection3 = false;
isInShelfArea = false;
Debug.Log("Position 1");
}
else if (collision.gameObject.name == "position_2")
{
isInSection1 = false;
isInSection2 = true;
isInSection3 = false;
isInShelfArea = false;
Debug.Log("Position 2");
}
else if (collision.gameObject.name == "position_3")
{
isInSection1 = false;
isInSection2 = false;
isInSection3 = true;
isInShelfArea = false;
Debug.Log("Position 3");
}
else if (collision.gameObject.name == "shelf")
{
isInSection1 = false;
isInSection2 = false;
isInSection3 = false;
isInShelfArea = true;
Debug.Log("shelf");
}
}
// Update is called once per frame
void Update()
{
if (isDragging)
{
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
transform.Translate(mousePosition);
}
else
{
if (isInSection1)
{
Debug.Log(section1Full);
if (section1Full == true)
{
transform.position = new Vector3(GameObject.Find(this.name + "_position").GetComponent<BoxCollider2D>().transform.position.x, GameObject.Find(this.name + "_position").GetComponent<BoxCollider2D>().transform.position.y, old_z ); ;
return;
}
else
{
transform.position = new Vector3(GameObject.Find("position_1").GetComponent<BoxCollider2D>().transform.position.x, GameObject.Find("position_1").GetComponent<BoxCollider2D>().transform.position.y, this.transform.position.z); ;
return;
}
}
else if (isInSection2)
{
transform.position = new Vector3(GameObject.Find("position_2").GetComponent<BoxCollider2D>().transform.position.x, GameObject.Find("position_2").GetComponent<BoxCollider2D>().transform.position.y, this.transform.position.z);
}
else if (isInSection3)
{
transform.position = new Vector3(GameObject.Find("position_3").GetComponent<BoxCollider2D>().transform.position.x, GameObject.Find("position_3").GetComponent<BoxCollider2D>().transform.position.y, this.transform.position.z);
}
else if (isInShelfArea)
{
transform.position = new Vector3(GameObject.Find(this.name + "_position").GetComponent<BoxCollider2D>().transform.position.x, GameObject.Find(this.name + "_position").GetComponent<BoxCollider2D>().transform.position.y, this.transform.position.z);
}
}
}
}
Your answer
Follow this Question
Related Questions
drag and merge object 0 Answers
Issues with collider or raycast 0 Answers
Issues with dropping an object on multiple objects separetly - Simple 2d Drag & Drop 0 Answers
RectangleContainsScreenPoint works backwards. 0 Answers
2D drag script Need Help! 1 Answer