Last active
December 7, 2021 13:20
-
-
Save TheCuttlefish/d415d3cb7ee53364c06dfb70e2a05990 to your computer and use it in GitHub Desktop.
Pack Master - collision check mechanic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class CollisionCheck : MonoBehaviour | |
{ | |
public bool isInside = false; | |
public bool isOverlapping = false; | |
private void Update() | |
{ | |
if (!isOverlapping && !isInside) GetComponent<SpriteRenderer>().color = Color.gray; | |
if (isOverlapping && !isInside) GetComponent<SpriteRenderer>().color = Color.blue; | |
if (!isOverlapping && isInside) GetComponent<SpriteRenderer>().color = Color.red; | |
if (isOverlapping && isInside) GetComponent<SpriteRenderer>().color = Color.green; | |
} | |
private void OnTriggerStay2D(Collider2D collision) | |
{ | |
if (isInside && collision.name == "item") | |
{ | |
isOverlapping = false; | |
} | |
if(collision.name == "container") | |
{ | |
if (collision.bounds.Contains(GetComponent<Collider2D>().bounds.min) | |
&& collision.bounds.Contains(GetComponent<Collider2D>().bounds.max)) | |
{ | |
isInside = true; | |
isOverlapping = true; | |
} | |
else | |
{ | |
isInside = false; | |
} | |
} | |
} | |
private void OnTriggerExit2D(Collider2D collision) | |
{ | |
if (collision.name == "container") | |
{ | |
isInside = false; | |
isOverlapping = false; | |
} | |
if (isInside && collision.name == "item") | |
{ | |
isOverlapping = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment