using UnityEngine; public abstract class YummyBase : MonoBehaviour { public AudioClip bounceSound; public AudioClip obtainedSound; public AudioClip missedSound; private AudioSource audioSource; private Animator animator; private SpriteRenderer spriteRenderer; private Vector2 speed; private bool isObtained = false; private float missedTimer = 0f; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { animator = GetComponent(); audioSource = GetComponent(); spriteRenderer = GetComponent(); speed = new Vector2(Random.Range(-0.05f, 0.05f), Random.Range(-0.05f, 0.05f)).normalized * 0.05f; } // Update is called once per frame void FixedUpdate() { if (isObtained) { return; } transform.Translate(speed); missedTimer += Time.fixedDeltaTime; if (missedTimer >= 5f) { audioSource.PlayOneShot(missedSound); animator.SetTrigger("missed"); } } void OnTriggerEnter2D(Collider2D other) { if (isObtained) { return; } if(other.gameObject.CompareTag("Bar")) { audioSource.PlayOneShot(obtainedSound); var player = GameObject.FindGameObjectWithTag("Player"); if (player != null) { ObtainedCallback(player.GetComponent()); } isObtained = true; spriteRenderer.enabled = false; } } void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Wall")) { ContactPoint2D contact = collision.GetContact(0); speed = Vector2.Reflect(speed, contact.normal); audioSource.PlayOneShot(bounceSound); } else { Physics2D.IgnoreCollision(collision.collider, GetComponent(), true); } } public void DestroyYummy() { Destroy(gameObject); } protected abstract void ObtainedCallback(Player player); }