The game I made in the assignment is actually not a “fake” game that only exist in the advertisement. This is actually a simple game in google. User could play it when the webpage has trouble connecting to the Internet.
This is the prototype I make. The dinosaur will jump over the cactus in the game. When he crashes on the cactus, the game would over. I have’t figure out how to count the score yet.
Development Log
1. Obtain images
I obtain images from the google webpage and import them into unity.
2. Things I have on the scene
a. Ground
I added ground object and also add a BoxCollider2D to the ground so that the dinosaur could landed on the ground.
b. Cloud
I added some clouds for decorating the scene.
c. Dinosaur
I added Rigidbody2D and BoxCollider2D to the dinosaur so that the dinosaur could landed on the ground and also collide with some other things (like cactus). I also added animation so that the leg of the dinosaur is moving.
d. Cactus Spawn
I originally tried to made this game by creating a very long ground with many cactus. However, this is obviously not efficient. So I created a cactus spawn so that new cactus would continuously be generated and old cactus would be destroyed. The detailed method would be further discusses in the challenge part.
e. Game Manager
This is mainly used to show the GAME OVER scene when the dinosaur crashed on the cactus and restart the game when user clicked restart button.
f. Game Over panel
This is a panel containing GAME OVER text and a restart button which will be shown when the dinosaur crashed on the cactus.
3. Scripts
a. Dinosaur
In this script, I mainly realized two things: 1. let the dinosaur jump when the Space is pressed; 2. detect the collision when the dinosaur crashes on the cactus.
b. Movement
The movement script mainly controls the movement of the ground, cloud and cactus. They are moving leftward and viewers would feel that the dinosaur is moving rightward. Also, the old cactus object would be destroyed.
c. GameManager
In GameManager script, I mainly realized 3 things: 1. generate new cactus object; 2. show GAME OVER when dinosaur crashes on the cactus; 3. restart the game when the restart button is pressed.
Achievement & Challenges
1. “Recycling” the ground and cloud
I originally wanted to create many clouds and a very long ground so that the dinosaur could moving forward for a long time. However, mo matter how long the ground is, it would eventually ended. And it is also not very efficient to include many cloud objects. So I “recycle” them by returning to the “start position” when they reaches “end position”.
void Update() { transform.position = new Vector2(transform.position.x - movementSpeed * Time.deltaTime, transform.position.y); if (transform.position.x <= endPos) { if (gameObject.tag == "Cactus") { Destroy(gameObject); } else { transform.position = new Vector2(startPos, transform.position.y); } } }
2. Continuously creating new cactus and destroying old
Similarly, it is not efficient to create many cactus in advance and putting all of them in the scene. So in the GameManager script, I set a certain spawn time for new cactus to be created.
void Update() { timer += Time.deltaTime; if (timer >= spawnTime) { Instantiate(cactus,cactusSpawnPos.transform); timer = 0; } }
Also, in the red part in the code of challenge 1, I destroyed the “used” cactus so that they will no longer exist in the scene after they are “used”.
3. Avoid jumping like a flappy bird
In my initial settings, the dinosaur will be given a “upward force” when the Space is pressed. So when a keep pressing the Space really fast, the dinosaur would fly like a flappy bird. So I added a boolean variable isJumping to solve this problem.
void Update() { if(Input.GetKeyDown("space") && isJumping == false) { rb.velocity = new Vector2(0, jump); isJumping = true; } } private void OnCollisionEnter2D(Collision2D collision) { isJumping = false; if (collision.gameObject.tag == "Cactus") { gm.GameOver(); } }
When the dinosaur “collides” with the ground, isJumping would be false. Only when pressing Space and isJumping == false are both satisfied, the dinosaur can be given an upward force to jump once. Then the isJumping would become true until the dinosaur lands on the ground again.
4. Show GAME OVER
I put the GAME OVER text and restart button in one panel. When the dinosaur collides with the cactus (realized in the blue part above), the time would be stopped and the panel would be visible in the GameOver function in GameManager script.
public void GameOver() { Time.timeScale = 0; GameOverScene.SetActive(true); }
When the restart button is pressed, the scene (which I have saved in the scene folder) would be load again and the time would start again.
public void Restart() { SceneManager.LoadScene("din_scene"); }
5. Final score
I want to record the score of the player, that is, count the number of the cactus that the dinosaur has jumped through. But I’m still figuring out how to realize this.
Leave a Reply