Lives and Losing 2D UFO game: PlayerController script coding

Analysis

The first line references the float variable of the player’s speed. Which is how the script calculates how fast the player moves.

The next line shows the public text variable calculating the count text displaying the number of collected Pick Ups. This algorithm will also be used to calculate the solution for the number of lives for the GUI text.

The next line shows the Win Text public conditional referencing to displaying the text at the end of the game “You Win” This type of code will be needed when working out the solution for the GUI text displaying You Lose when the player runs out of lives. As both these conditionals would use the UI or GUI text component for winning and losing.

The next line shows the private Rigidbody2D variable which can reference the component creating the 2D physics.

The last line, private int count, u= references to the integer storing the pickup number at each point of the game. A similar code will be needed when showing the number of lives as well.

* The first line references the float variable of the player’s speed. Which is how the script calculates how fast the player moves.

* The next line references where the Rigidbody2D component is in the programming of the game.

* The next line initialises the text count to zero for the start of the game. Which will be similar when initialising the solution for having the player start with 5 lives. There will need to be a count for lives at 5 instead of at 0.

* The win text line initialises the win text to have no value because it’s the start of the game. For the You Lose solution, the code should also initialise no Lose text until the player has lost.

* The final line is a function to update the text with the current count value which means the player’s lives will need to update to set the count text of lives.

* The initial line of this part of the code is written for the fixed time and specific frame rate.

* The forthcoming line of code references the character’s movement to float horizontal along the axis.

* The third line of code references the character’s movement as vertical and is inputted for the vertical axis.

* The next line of code programs the game to create a variable of the horizontal and vertical movements.

* The end of the movement algorithm multiplies the movement and speed together for the character to navigate through the game.

* The OnTriggerEnter2D is a conditional programmed when the object is triggered by a collider. For the player taking damage, the solution would be that the player would use a trigger collider when colliding with an enemy instead of colliding with a Pick Up.

* The second line of code checks whether the Collider2D parameter is tagged PickUp and if it is then the Pick up can become inactive from the collision. This would also be needed when the parameter checks enemies to see if they’re tagged enemy instead of Pick Up.

* The third line of code is irrelevant because the sound manager is not necessary when implementing the health mechanic.

* The next line programs the game to add one to the count variable. This would be similar to the player’s lives count because the player would be losing one from their count variable.

* The final line updates the current count by the SetCountText function. When the player loses a life, it would be updated and displayed in a similar SetCountText.

* This last algorithm begins with the code updating the countText and displaying the victory message. Which would also be used when working out the solution to update the text to the losing message.

* After that, the code sets the countText object text property to count as well as the number in the count variable.

* Then the code checks if all pick ups are collected and displays the winText displaying You Win! It could also be used in the solution to show the number of lives when the player loses the game.

* Then finally for the solution the program’s lose text would be set to “You Lose” instead of “You Win”.

Synthesis

The Solution

using UnityEngine;

using System.Collections;

/Adding this allows us to access members of the UI namespace including Text.

using UnityEngine.UI;

public class CompletePlayerController : MonoBehaviour {

public float speed; //Floating point variable to store the player's movement speed.

public Text countText; //Stores a reference to the UI Text component which will display the number of pickups.

public Text livesText; // Stores a reference to the UI test component which will display the number of lives.

public Text loseText; //Store a reference to the UI Text component which will display the 'You lose' message

private Rigidbody2D rb2d; //Store a reference to the Rigidbody2D component required to use 2D Physics.

private int count

private int lives; //Integer to store the number of lives so far.

public AudioClip collectSound;

// Use this for initialization

void Start() {

rb2d = GetComponent<Rigidbody2D> (); //Get and store a reference to the Rigidbody2D component so that we can access it.

//Initialize count to zero.

count = 0; //Initialize count to zero.

lives = 5; //Initialize lives count to zero.

loseText.text =""; //Initialise loseText to a blank string since we haven't lost any lives yet at the beginning.

SetCountText (); } //Call our SetCountText function which will update the text with the current value for count.

SetLivesText(); //Call our SetLivesText

SetCountText();

}

//FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.

void FixedUpdate()

{

//Store the current horizontal input in the float moveHorizontal.

float moveHorizontal = Input.GetAxis ("Horizontal");

//Store the current vertical input in the float moveVertical.

float moveVertical = Input.GetAxis ("Vertical");

//Use the two store floats to create a new Vector2 variable movement.

Vector2 movement = new Vector2 (moveHorizontal, moveVertical);

//Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.

rb2d.AddForce (movement * speed); }

void OnTriggerEnter2D(Collider2D other)

//Check the provided Collider2D parameter other to see if it is tagged "Enemy", if it is...

if (other.gameObject.CompareTag ("Enemy"))

//decreases one to the current value of our count variable.

count = count - 1;

//Update the currently displayed count by calling the SetCountText

function.

SetCountText();

//This function updates the text displaying the number of lives we've lost and displays our losing message if we've lost all the lives.

void SetCountText() {

//Set the text property of our our countText object to "Count: " followed by the number stored in our count variable.

countText.text = "Count: " + count.ToString ();

//Check if we have lost all lives. If we have...

if (count >= 0)

//... then set the text property of our loseText object to "You Lose!"

loseText.text = "You Lose!";

}

Evaluation

References

  1. Monodevelop
  2. Assessment Item A
  3. Task Sheet
  4. Adobe Photos

Credits:

Created with images by marioschulz - "ufo alien sky" • qimono - "philatelist stamp collection stamp" • YeriLee - "highlighter coloring fluorescent" • audi_insperation - "Reflecting"

Report Abuse

If you feel that this video content violates the Adobe Terms of Use, you may report this content by filling out this quick form.

To report a Copyright Violation, please follow Section 17 in the Terms of Use.