wall_smash/Assets/Scripts/Bouncy_Ball.cs
2025-04-15 13:34:19 +05:30

57 lines
1.3 KiB
C#

using TMPro;
using UnityEngine;
public class Bouncy_Ball : MonoBehaviour
{
public TMP_Text ScoreText;
public GameObject[] livesImage;
public float minY = -5.5f;
public float maxVelocity = 15f;
Rigidbody2D rb;
int score = 0;
int lives =5;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if(transform.position.y < minY)
{
if(lives <= 0)
{
GameOver();
}else
{
transform.position = Vector3.zero;
rb.linearVelocity = Vector3.zero;
lives--;
livesImage[lives].SetActive(false);
}
}
if(rb.linearVelocity.magnitude > maxVelocity)
{
rb.linearVelocity = Vector3.ClampMagnitude(rb.linearVelocity, maxVelocity);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Brick"))
{
Destroy(collision.gameObject);
score += 10;
ScoreText.text = score.ToString("00000");
}
}
public void GameOver()
{
Debug.Log("GameOver");
}
}