Cross Rush
At a Glance
Summary

Cross Rush is a game that I made to polish my mobile development skills and possibly make some money. While developing the game itself wasn’t so difficult, learning how to integrate google ads into the game was a bit tricky. Initially I published the game to the Apple App Store and Google Play Store, however it is no longer available on either platform. The goal is the game is to survive by dodging the oncoming obstacles. The circle moves forward while the player taps the screen to change Its direction.

Player.cs Example

				
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

	float speed = 0;
	float baseSpeed = 5;

	Transform cam;
	Transform ghostPlayer;
	Transform trail;

	float camWidth;
	float playerSize;

	ParticleSystem trailSys;

	Level level;
	Fade fade;
	CountDown countDown;

	Menu menu;

	[SerializeField] GameObject explosion;

	bool alive = false;

	void Start () {
		menu = GameObject.Find("Main Camera").GetComponent<Menu>();
		level = GameObject.Find("Obstacles").GetComponent<Level>();
		countDown = GameObject.Find("countDown").GetComponent<CountDown>();
		fade = GameObject.Find("Obstacles").GetComponent<Fade>();
		cam = GameObject.Find("Main Camera").transform;
		camWidth = Camera.main.ViewportToWorldPoint(new Vector3(1,0,1)).x;
		ghostPlayer = GameObject.Find("GhostPlayer").transform;
		trail = GameObject.Find("trail").transform;
		playerSize = GetComponent<Renderer>().bounds.size.x;
		trailSys = trail.GetComponent<ParticleSystem>();
		transform.eulerAngles = new Vector3(0,0,0);
	}

	void Update () {
		if(!alive) return;

        // move player forward
		transform.position = new Vector3(transform.position.x + (speed * Time.deltaTime), transform.position.y, transform.position.z);
		cam.position = Vector3.Lerp(cam.position, new Vector3(0,transform.position.y,-10), .2f);

		trail.position = new Vector3(transform.position.x, transform.position.y, 1);

        // move player to other side if they exit screen
		if(transform.position.x + playerSize / 2 >= camWidth) {
			ghostPlayer.transform.position = new Vector3(transform.position.x - camWidth * 2, transform.position.y, 0);
			trail.position = new Vector3(ghostPlayer.position.x, ghostPlayer.position.y, 1);
			ghostPlayer.GetComponent<Renderer>().enabled = true;

			if(transform.position.x  - playerSize / 2 >= camWidth) {
				transform.position = new Vector3(ghostPlayer.position.x, transform.position.y, 0);
				ghostPlayer.GetComponent<Renderer>().enabled = false;
			}
		}
		else if(transform.position.x - playerSize / 2 <= -camWidth) {
			ghostPlayer.transform.position = new Vector3(transform.position.x + camWidth * 2, transform.position.y, 0);
			trail.position = new Vector3(ghostPlayer.position.x, ghostPlayer.position.y, 1);
			ghostPlayer.GetComponent<Renderer>().enabled = true;

			if(transform.position.x  + playerSize / 2 <= -camWidth) {
				transform.position = new Vector3(ghostPlayer.position.x, transform.position.y, 0);
				ghostPlayer.GetComponent<Renderer>().enabled = false;
			}
		}
		else {
			ghostPlayer.GetComponent<Renderer>().enabled = false;
		}

		float ypos = transform.position.y;
		float xpos = transform.position.x;

		foreach( Touch touch in Input.touches) {
			if(touch.phase == TouchPhase.Began) {
				if(menu.GetMenu() != 4)
				{
                    // switch player direction
					if(transform.eulerAngles.z == 0) {
						if(touch.position.x < Screen.width / 2) {
							transform.eulerAngles = new Vector3(0,0,45);
							speed = baseSpeed;
						} else if(touch.position.x >= Screen.width / 2) {
							transform.eulerAngles = new Vector3(0,0,-45);
							speed = -baseSpeed;
						}
					}

					speed = -speed;
					transform.eulerAngles = new Vector3(0,0,-transform.eulerAngles.z);
					transform.position = new Vector3(xpos, ypos,transform.position.z);
				}
			}
		}
	}

    // kill player if they hit obstacle
	void OnTriggerEnter2D (Collider2D c) {
        if (c.gameObject.tag != "Obstacle") return;

		alive = false;
		GetComponent<AudioSource>().Play();
		Instantiate(explosion, transform.position, Quaternion.identity);
		level.OnPlayerDies();
		ghostPlayer.GetComponent<SpriteRenderer>().enabled = false;
		trailSys.enableEmission = false;
		GetComponent<SpriteRenderer>().enabled = false;
		StartCoroutine(Reset(false));
	}

    // reset player & level for new round
	public IEnumerator Reset (bool fromMain) {
		if(!fromMain) yield return new WaitForSeconds(2);

		fade.FadeOut();
		Menu menu = GameObject.Find("Main Camera").GetComponent<Menu>();
		Color playerColor = menu.GetColor();
		GetComponent<SpriteRenderer>().color = playerColor;
		ghostPlayer.GetComponent<SpriteRenderer>().color = playerColor;
		trailSys.startColor = playerColor;

		yield return new WaitForSeconds(.5f);

		level.Initialize();
		countDown.Initialize();
		fade.FadeIn();
		speed = 0;
		transform.eulerAngles = new Vector3(0,0,0);
		transform.position = new Vector3(0, 0, 0);
		alive = true;
		GetComponent<SpriteRenderer>().enabled = true;
		trailSys.enableEmission = true;
	}
}