Scriptum/Assets/breakable.cs
2022-04-25 21:41:47 +02:00

51 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class breakable : MonoBehaviour
{
public Sprite[] sprites;
private int oldSprite;
private int newSprite;
private List<int> availableSprites = new List<int>();
void Start()
{
oldSprite = 0;
for(int i=0; i<3; i++)
{
availableSprites.Add(i);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
if (oldSprite == 0)
{
newSprite = availableSprites[1];
oldSprite = 1;
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
}
else if (oldSprite == 1)
{
newSprite = availableSprites[2];
oldSprite = 2;
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
}
else if (oldSprite == 2)
{
Destroy(gameObject);
}
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
}
}
}