using UnityEngine; /// /// Represents a single bubble, handling its movement, wobble effect, scaling, and sprite assignment. /// public class Bubble : MonoBehaviour { public float speed = 1f; public float wobbleSpeed = 1f; private SpriteRenderer spriteRenderer; private SpriteRenderer bottleSpriteRenderer; private float timeOffset; private float minScale = 0.2f; private float maxScale = 1.2f; void Awake() { // Cache references and randomize time offset for wobble spriteRenderer = GetComponent(); timeOffset = Random.value * 100f; // Find the child named "BottleSprite" and get its SpriteRenderer Transform bottleSpriteTransform = transform.Find("BubbleSprite"); if (bottleSpriteTransform != null) { bottleSpriteRenderer = bottleSpriteTransform.GetComponent(); } } void Update() { // Move bubble upward transform.position += Vector3.up * speed * Time.deltaTime; // Wobble effect (smooth oscillation between min and max scale) float t = (Mathf.Sin((Time.time + timeOffset) * wobbleSpeed) + 1f) * 0.5f; // t in [0,1] float newScale = Mathf.Lerp(minScale, maxScale, t); transform.localScale = Vector3.one * newScale; // Destroy when off screen if (transform.position.y > Camera.main.orthographicSize + 2f) { Destroy(gameObject); } } /// /// Sets the main sprite for the bubble. /// /// Sprite to assign. public void SetSprite(Sprite sprite) { if (spriteRenderer != null) spriteRenderer.sprite = sprite; } /// /// Sets the sprite for the child "BottleSprite" renderer. /// /// Sprite to assign. public void SetBottleSprite(Sprite sprite) { if (bottleSpriteRenderer != null) bottleSpriteRenderer.sprite = sprite; } /// /// Sets the minimum and maximum scale for the wobble effect. /// /// Minimum scale. /// Maximum scale. public void SetWobbleScaleLimits(float min, float max) { minScale = min; maxScale = max; } }