AGAR
Plan:
-Learn about 2D game development in Unity
-Create a player sprite and controller
-Randomly spawn food particles
-Enlarge the player when they eat these.
-Learn about 2D game development in Unity
-Create a player sprite and controller
-Randomly spawn food particles
-Enlarge the player when they eat these.
Let's steal another game! This time, Agario!
As we always start, create a new project. This time, set it up for 2D. (shown in the image). Then, click "Create Project".
Next, make sure that it is indeed 2D by ensuring that the "2D" button (circled in red below) is selected.
Save the scene, and create a new sprite by clicking GameObject -> 2D Object -> Sprite.
Move the Main Camera to (0, 0, -10), make the new sprite a child of the camera, rename the new sprite "Player", and in its inspector, set the "Sprite" field in the "Sprite Renderer" component to "Knob" and resize its transform to (5, 5, 1). Just like that, a circle should appear in the centre of your screen.
Create your "Scripts" folder and a new C# script called "RandomColour" (or "RandomColor" if you choose to spell that way). This script will randomise the colour of the player.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SpriteRenderer))]
public class RandomColour : MonoBehaviour {
[Range(0f, 1f)]
public float minValue;
[Range(0f, 1f)]
public float maxValue;
void Start () {
GetComponent<SpriteRenderer> ().color = new Color (Random.Range (minValue, maxValue), Random.Range (minValue, maxValue), Random.Range (minValue, maxValue));
}
}
Save the script and attach it to the Player. Set MinValue to zero and MaxValue to one. Play the scene a few times and the player will be a random colour every time.
-[RequireComponent(typeof(SpriteRenderer)] will cause the script to return an error message if the GameObject does not have a SpriteRenderer attached to it.
-[Range(min, max)] gives you a slider in the editor that lets you set the number between min and max.
-GetComponent<>() should be familiar.
-new Color (...); sets the colour of the SpriteRenderer to a new colour:
-Red: Random number between minValue and maxValue
-Green: Random number between minValue and maxValue
-Blue: Random number between minValue and maxValue
-[RequireComponent(typeof(SpriteRenderer)] will cause the script to return an error message if the GameObject does not have a SpriteRenderer attached to it.
-[Range(min, max)] gives you a slider in the editor that lets you set the number between min and max.
-GetComponent<>() should be familiar.
-new Color (...); sets the colour of the SpriteRenderer to a new colour:
-Red: Random number between minValue and maxValue
-Green: Random number between minValue and maxValue
-Blue: Random number between minValue and maxValue
So there's our random-colour script to make our player a random colour. But a player is nearly useless in this situation if it can't move.
Create a background by creating a plane, moving it to (0, 0, 1), rotating it to (270, 0, 0), and scaling it to (100, 0, 100). Create a new material using the image below (right-click -> Save As -> in the Assets folder) and settings below, and apply it to the plane.
Rotate the directional light to (0, 0, 0), and set its colour to white.
Create a background by creating a plane, moving it to (0, 0, 1), rotating it to (270, 0, 0), and scaling it to (100, 0, 100). Create a new material using the image below (right-click -> Save As -> in the Assets folder) and settings below, and apply it to the plane.
Rotate the directional light to (0, 0, 0), and set its colour to white.
We need a control script. Create a new C# script and name it "PlayerController".
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed = 0.1f;
void Update () {
Vector3 t = new Vector3 (Input.mousePosition.x - (Screen.width / 2), Input.mousePosition.y - (Screen.height / 2));
transform.position = Vector3.MoveTowards (transform.position, t, moveSpeed);
}
}
Breaking it down:
-Declaring a float called "moveSpeed" with a default value of 0.1
-Creating a new Vector3 called "t" that deals with irritating mouse position stuff, don't worry about this.
-Setting the position to (moveSpeed) closer to t
-Declaring a float called "moveSpeed" with a default value of 0.1
-Creating a new Vector3 called "t" that deals with irritating mouse position stuff, don't worry about this.
-Setting the position to (moveSpeed) closer to t
Attach PlayerController to the player, and play the scene. You should be able to move the mouse around and the player should follow. We now need something for the player to eat. Create a new circle in the same method that we made the player, but shrink it to (2, 2, 1). Call it "Food". Be sure to also add our random-colour script, and a Circle Collider. Also, add a trigger Circle Collider to the player. Create a new "Prefabs" folder and drag in the Food. Now, delete "Food" from the Hierarchy.
Create a new empty GameObject, set its position to (0, 0, 0), and call it "Handler." Create a new script called SpawnFood, and attach it to the Handler. Then, change the code of SpawnFood to this:
using UnityEngine;
using System.Collections;
public class SpawnFood : MonoBehaviour {
public GameObject ParticleOfFood;
public int StartingParticles;
public float CoordinateLimit;
public float SecondsBetweenSpawns;
void Start () {
for (int i = 0; i < StartingParticles; i++) {
Instantiate (ParticleOfFood, new Vector3 (Random.Range (-CoordinateLimit, CoordinateLimit), Random.Range (-CoordinateLimit, CoordinateLimit)), Quaternion.identity).name = "Food";
}
StartCoroutine (SpawnDuringGame ());
}
IEnumerator SpawnDuringGame () {
Instantiate (ParticleOfFood, new Vector3 (Random.Range (-CoordinateLimit, CoordinateLimit), Random.Range (-CoordinateLimit, CoordinateLimit)), Quaternion.identity).name = "Food";
yield return new WaitForSeconds (SecondsBetweenSpawns);
StartCoroutine (SpawnDuringGame ());
}
}
Be careful when copy/pasting the above code, some is clipped off some screens.
The only unfamiliar code in here should be coroutines.
-In Start(), we're starting the Coroutine called SpawnDuringGame().
-In SpawnDuringGame(), we're instantiating the food particles, naming them "Food", then waiting for SecondsBetweenSpawns, then running SpawnDuringGame() again.
The only unfamiliar code in here should be coroutines.
-In Start(), we're starting the Coroutine called SpawnDuringGame().
-In SpawnDuringGame(), we're instantiating the food particles, naming them "Food", then waiting for SecondsBetweenSpawns, then running SpawnDuringGame() again.
Create a new script, called "EatFood". Attach it to the player. Also, add a Rigidbody2D and set it as follows:
Now, edit EatFood.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class EatFood : MonoBehaviour {
public Text text;
void OnTriggerEnter2D(Collider2D col){
if (col.gameObject.name == "Food") {
Destroy (col.gameObject);
float area = Mathf.PI * Mathf.Pow ((transform.lossyScale.x / 2), 2f);
area += Mathf.PI;
float newCircumference = Mathf.Sqrt ((area / Mathf.PI)) * 2;
transform.localScale = new Vector3 (newCircumference, newCircumference, 1f);
text.text = Mathf.Round (area).ToString();
}
}
}
"public Text text". We need text. Do this:
... and then this...
Attach EatFood to the player if you haven't already, and click-and-drag the Text GameObject we just created into the "text" field in EatFood's inspector.
Click Play.
Click Play.