I created this game where the player controls a spaceship to navigate through outer space. Points are collected for every planet collected, but the game is over when the player collides with an alien.
Play the game: https://reallycoolgames.itch.io/space-adventure
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
public class spawnManager : MonoBehaviour
{
public GameObject[] enemies;
public GameObject[] stars;
//make private later
public float zSpawn = -0.9f;
public float xSpawnRange = 12.3f;
public float ylowSpawn = -3.8f;
public float yhighSpawn = 5.8f;
public float starSpawnTime = 3.0f;
public static float enemySpawnTime = 0.5f;
public static float startDelay = 1.0f;
// Start is called before the first frame update
public void Start()
{
InvokeRepeating("SpawnRandomEnemy", startDelay, enemySpawnTime);
InvokeRepeating("SpawnRandomStar", startDelay, enemySpawnTime);
}
// Update is called once per frame
void Update()
{
}
public void SpawnRandomEnemy()
{
float randomX = Random.Range(-xSpawnRange, xSpawnRange);
int randomIndex = Random.Range(0, enemies.Length);
UnityEngine.Vector3 spawnPos = new UnityEngine.Vector3(randomX, yhighSpawn, zSpawn);
Instantiate(enemies[randomIndex], spawnPos, enemies[randomIndex].gameObject.transform.rotation);
}
public void SpawnRandomStar()
{
float randomX = Random.Range(-xSpawnRange, xSpawnRange);
int randomIndex = Random.Range(0, stars.Length);
UnityEngine.Vector3 spawnPos = new UnityEngine.Vector3(randomX, yhighSpawn, zSpawn);
Instantiate(stars[randomIndex], spawnPos, stars[randomIndex].gameObject.transform.rotation);
}
}
Move Left:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveLeft : MonoBehaviour
{
public float speed = 0.5f;
public float xDestroy = -12.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//moves to the left
transform.Translate(Vector3.left * speed);
// destroys when out of bounds
if (transform.position.x < xDestroy)
{
Destroy(gameObject);
}
}
}
Move Right:
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
public class moveRight : MonoBehaviour
{
public float speed = 0.5f;
public float xDestroyright = 12.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//moves to the right
transform.Translate(UnityEngine.Vector3.right * speed);
if (transform.position.x > xDestroyright)
{
Destroy(gameObject);
}
}
}
Move down:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveDown : MonoBehaviour
{
public float speed = 5.0f;
private float yDestroy = -3.7f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//moves to the left
transform.Translate(Vector3.down * speed);
//destroys if out of bounds
if (transform.position.y < yDestroy)
{
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using UnityEngine;
using TMPro;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public float speed = 5.0f; //clean up later, make all private
public float topBound = 6.0f;
public float lowerBound = -3.7f;
public float verticalBound = 12;
private Rigidbody playerRb;
private int score;
public TextMeshProUGUI scoreText;
public ParticleSystem pointParticle;
public ParticleSystem aliencollision;
public TextMeshProUGUI gameOverText;
public static bool GameActive;
public Button playAgain;
public Button startGame;
public GameObject titleScreen;
public Button Exit;
// Start is called before the first frame update
void Start()
{
GameActive = false;
UpdateScore(0);
playerRb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (GameActive)
{
playerMovement();
boundaries();
}
}
// gets the movement of the player from arrow keys
void playerMovement()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// moves the player up and down
transform.Translate(UnityEngine.Vector3.up * Time.deltaTime * speed * verticalInput);
//moves the player left and right
transform.Translate(UnityEngine.Vector3.right * Time.deltaTime * speed * horizontalInput);
}
// sets the boundaries of the player
void boundaries()
{
if (transform.position.y < lowerBound)
{
transform.position = new UnityEngine.Vector3(transform.position.x, lowerBound, transform.position.z);
}
else if (transform.position.y > topBound)
{
transform.position = new UnityEngine.Vector3(transform.position.x, topBound, transform.position.z);
}
else if (transform.position.x < -verticalBound)
{
transform.position = new UnityEngine.Vector3(-verticalBound, transform.position.y, transform.position.z);
}
else if (transform.position.x > verticalBound)
{
transform.position = new UnityEngine.Vector3(verticalBound, transform.position.y, transform.position.z);
}
}
//finally fucking works after like 3hrs of debugging
//use chained conditionals in onTrigger, Oncollider doesn't work
private void OnTriggerEnter(Collider other)
{
if (GameActive)
{
if (other.gameObject.CompareTag("Star"))
{
Destroy(other.gameObject);
UpdateScore(5);
Instantiate(pointParticle, transform.position, pointParticle.transform.rotation);
}
if (other.gameObject.CompareTag("Enemy"))
{
if (GameActive)
{
Instantiate(aliencollision, transform.position, aliencollision.transform.rotation);
}
Destroy(other.gameObject);
Debug.Log("Player has collided with the enemy.");
gameOverText.gameObject.SetActive(true);
playAgain.gameObject.SetActive(true);
GameActive = false;
UpdateScore(0);
}
}
}
private void UpdateScore(int scoreToAdd)
{
score += scoreToAdd;
scoreText.text = "Score: " + score;
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
GameActive = true;
}
public void destroyButton()
{
GameActive = true;
titleScreen.gameObject.SetActive(false);
}
public void doExitGame()
{
Application.Quit();
Debug.Log("Game is closing");
}
}