void OnTriggerEnter2D(Collider2D collision) { if (Status == MiningMachineStatus.Drop) { if (collision.tag == "Wall") { Status = MiningMachineStatus.Drag; } else if (collision.tag == "Treasures") { Treasure treasure = collision.transform.GetComponentInParent <Treasure>(); float treasureHalfWidth = treasure.GetComponent <SpriteRenderer>().sprite.bounds.size.x * 0.5f; treasure.transform.position = hook.transform.position + hook.transform.right * treasureHalfWidth; treasure.transform.SetParent(hook.transform); DragTreasure = treasure; Status = MiningMachineStatus.Drag; } else if (collision.tag == "Chest") { Status = MiningMachineStatus.Drag; } } }
public void ExplodeTreasure(Treasure treasure, int pieces, Vector3 playerPosition) { if ((treasure.transform.localScale.x / (float)pieces) + 0.1f < minDestructionSize) { // if the pieces will be too small, don't explode return; } for (int i = 0; i < 2; i++) { GameObject spawnedTreasure; switch (treasure.color) { case GameManager.ItemColor.BLACK: spawnedTreasure = Instantiate(blackTreasure[GetSpriteIndex(GameManager.ItemColor.BLACK, treasure.GetComponent <SpriteRenderer>().sprite)]); break; case GameManager.ItemColor.BLUE: spawnedTreasure = Instantiate(blueTreasure[GetSpriteIndex(GameManager.ItemColor.BLUE, treasure.GetComponent <SpriteRenderer>().sprite)]); break; case GameManager.ItemColor.RED: spawnedTreasure = Instantiate(redTreasure[GetSpriteIndex(GameManager.ItemColor.RED, treasure.GetComponent <SpriteRenderer>().sprite)]); break; default: spawnedTreasure = null; break; } spawnedTreasure.transform.position = new Vector3(treasure.transform.position.x + (i - 0.25f), treasure.transform.position.y + (i - 0.25f), treasure.transform.position.z); spawnedTreasure.transform.SetParent(treasureParent); float scale = (treasure.transform.localScale.x / (float)pieces) + 0.1f; spawnedTreasure.transform.localScale = new Vector3(scale, scale, scale); currentTreasure.Add(spawnedTreasure.GetComponent <Treasure>()); spawnedTreasure.GetComponent <Rigidbody2D>().AddExplosionForce(500f, playerPosition, 20f); } currentTreasure.Remove(treasure); treasure.Explode(); }