Hey all, I am not quite making an inventory system, but it seems similar to one. I want to place an item into a slot and when that slot is full, then trying to put more items on top should collide with the physics of the filled slot and move out of the way rather than infinitely fill with more and more items stacking. I tried using booleans to make an isSlotFilled = true or false and check that but it wasn’t’ compatible with my code I have so far.
public class BoxSlot : MonoBehaviour
{
public List<GoodCheese> goodCheeses;
public List<BadCheese> badCheeses;
public GameObject goodCheese;
public GameObject badCheese;
public GameObject boxSlot;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "GoodCheese")
{
Debug.Log("Box Slot: Good Cheese!");
other.transform.position = this.transform.position;
}
if (other.gameObject.tag == "BadCheese")
{
Debug.Log("Box Slot: Bad Cheese!");
other.transform.position = this.transform.position;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "GoodCheese")
{
Debug.Log("Box Slot: Good Cheese Exited Trigger!");
transform.gameObject.tag = "slot";
}
if(other.gameObject.tag == "BadCheese")
{
Debug.Log("Box Slot: Bad Cheese Exited Trigger!");
}
}
}
public class GoodCheese : MonoBehaviour
{
public const string LAYER_NAME = "Default";
public const string LAYER_NAME2 = "PickUpInspect";
public int sortingOrder = 0;
private SpriteRenderer sprite;
public Rigidbody2D rb;
private Transform thisTF;
private bool isDragged = false;
private Vector3 mouseDragStartPosition;
private Vector3 spriteDragStartPosition;
void Start()
{
sprite = GetComponent<SpriteRenderer>();
rb = GetComponent<Rigidbody2D>();
thisTF = GetComponent<Transform>();
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "slot")
{
Debug.Log ("Good Cheese: I entered slot!");
gameObject.GetComponent<Rigidbody2D>().constraints =
RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezePositionY |
RigidbodyConstraints2D.FreezeRotation;
}
if (other.tag == "Zoom")
{
Debug.Log("Zooming in!");
Vector2 scaleUp = new Vector2(thisTF.localScale.x + 0.5f, thisTF.localScale.y + 0.5f);
thisTF.localScale = scaleUp;
}
}
public void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "slot")
{
rb.constraints = RigidbodyConstraints2D.None;
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
}
if (other.tag == "Zoom")
{
Vector2 scaleUp = new Vector2(thisTF.localScale.x - 0.5f, thisTF.localScale.y - 0.5f);
thisTF.localScale = scaleUp;
}
}
private void OnMouseDown()
{
this.gameObject.layer = LayerMask.NameToLayer("PickUpInspect");
sprite.sortingLayerName = LAYER_NAME2;
sprite.sortingOrder = 5;
isDragged = true;
mouseDragStartPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
spriteDragStartPosition = transform.localPosition;
}
private void OnMouseDrag()
{
if (isDragged)
{
transform.localPosition = spriteDragStartPosition +
(Camera.main.ScreenToWorldPoint(Input.mousePosition) - mouseDragStartPosition);
}
}
private void OnMouseUp()
{
this.gameObject.layer = LayerMask.NameToLayer("Default");
sprite.sortingLayerName = LAYER_NAME;
sprite.sortingOrder = 2;
isDragged = false;
}
}