using UnityEngine;
public class HamburgerGame : MonoBehaviour
{
public GameObject hamburgerPrefab; // hamburger prefab to spawn
public float spawnRate = 1.0f; // rate at which hamburgers spawn
public float moveSpeed = 5.0f; // speed at which hamburgers move
public int score = 0; // player's score
public int lives = 3; // number of lives the player has
private float spawnTimer; // timer for spawning hamburgers
void Update()
{
// decrement spawn timer
spawnTimer -= Time.deltaTime;
// if spawn timer is less than or equal to 0, spawn a hamburger
if (spawnTimer <= 0.0f)
{
SpawnHamburger();
spawnTimer = 1.0f / spawnRate;
}
// move all hamburgers in scene
foreach (GameObject hamburger in GameObject.FindGameObjectsWithTag("Hamburger"))
{
hamburger.transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
// check if hamburger is out of bounds and destroy it
if (hamburger.transform.position.x < -10.0f)
{
Destroy(hamburger);
lives--;
}
}
// check if player has pressed the space key to eat a hamburger
if (Input.GetKeyDown(KeyCode.Space))
{
// get list of all hamburgers in scene
GameObject[] hamburgers = GameObject.FindGameObjectsWithTag("Hamburger");
// check if player is close enough to eat a hamburger
foreach (GameObject hamburger in hamburgers)
{
if (Vector3.Distance(transform.position, hamburger.transform.position) < 2.0f)
{
// destroy hamburger and increase score
Destroy(hamburger);
score++;
}
}
}
// check if player has run out of lives
if (lives <= 0)
{
Debug.Log("Game Over!");
enabled = false;
}
}
// spawn a hamburger at a random position along the y-axis
void SpawnHamburger()
{
float y = Random.Range(-5.0f, 5.0f);
Instantiate(hamburgerPrefab, new Vector3(10.0f, y, 0.0f), Quaternion.identity);
}
}
Comments