I followed these two blog posts: post #1 and post #2, and updated it for the use with Unity 4.5 and added the counters for number of bullets fired and number of rocks generated and showed the precision efficiency.
You can try my version, or you can download the project on GitHub. Following are the steps on how to make it yourself:
- Start a new 2D project in Unity3D
- Create folders Scripts, Scenes, Prefabs, Textures
- Put all the texture assets to Textures folder
- Save the (ctrl +s) scene to Scenes folder with some name
- Drag background to Hierarchy
- Drag ship to Hierarchy
- Set
y
to -4
- Add
Rigidbody2D
- Check
IsKinematic
(the gravity doesn’t affect it)
- Add script to ship:
#pragma strict
public var bullet : GameObject;
public var brzina: int;
var score : int;
function Start(){
score = 0;
}
function Update() {
rigidbody2D.velocity.x = Input.GetAxis("Horizontal") * 10;
rigidbody2D.velocity.y = Input.GetAxis("Vertical") * 10;
transform.Translate(Input.acceleration.x*Time.deltaTime*20, 0, 0);
if (Input.GetKeyDown("space") || Input.GetMouseButtonDown(0)) {
Instantiate(bullet, transform.position, Quaternion.identity);
}
}
- Add bullet to Hierarchy
- Add
Physics2D
-> Rigidbody
2D - Add script:
public var speed : int = 6;
function Start () {
rigidbody2D.velocity.y = speed;
}
function OnBecameInvisible() {
Destroy(gameObject);
}
- Add bullet to Prefabs folder and delete it from the Hierarchy
- Drag the bullet from Prefabs folder to the bullet variable in Inspector when the spaceship is selected
- Add an enemy (from Textures) to the Hierarchy
- Add
Rigidbody
2D - Set
IsKinematic
- Add script:
public var speed : int = -5;
function Start () {
rigidbody2D.velocity.y = speed;
rigidbody2D.angularVelocity = Random.Range(-200, 200);
Destroy(gameObject, 3);
}
function OnTriggerEnter2D(obj : Collider2D) {
var name = obj.gameObject.name;
if (name == "bullet(Clone)") {
Destroy(obj.gameObject);
handleDestroy(gameObject);
}
if (name == "spaceship") {
handleDestroy(gameObject);
}
}
function handleDestroy(gameObject: GameObject){
gameObject.Find("ScoreText").SendMessage("Hit");
Destroy(gameObject);
}
- Add enemy from Hierarchy to Prefabs folder and delete it from Hierarchy
- Add
spawn
object to Hierarchy
- Position it above the background
- Add script:
public var enemy : GameObject;
public var spawnTime : float = 1.3;
function Start() {
InvokeRepeating("addEnemy", spawnTime, spawnTime);
}
function addEnemy() {
var x1 = transform.position.x - renderer.bounds.size.x/2;
var x2 = transform.position.x + renderer.bounds.size.x/2;
var spawnPoint = new Vector2(Random.Range(x1, x2), transform.position.y);
Instantiate(enemy, spawnPoint, Quaternion.identity);
}
- Drag enemy prefab to
spawn
object
- For the spaceship, the enemy prefab, and the bullet prefab, do the following:
- Add Component -> Physics 2D -> Box Collider 2D
- For enemy Box Collider 2D check
IsTrigger
and this will give you the OnTriggerEnter2D
function
- Create -> UI -> Text (name it
ScoreText
)
- Canvas -> RenderMode -> World space, no camera
- Rect transform – 450×340 (WxH)
- Add -> Script -> ScoreScript.js:
import UnityEngine.UI.Text;
var Counter : int;
var text : UnityEngine.UI.Text;
function Start () {
text = GetComponent(UnityEngine.UI.Text);
Counter = 0;
}
function Update () {
text.text = "Kills: "+Counter;
}
function Hit () {
Counter++;
}
- Create -> UI -> Text (name it
RocksText
)
- Add -> Script -> RocksScript.js:
import UnityEngine.UI.Text;
var Counter : int;
private var text : UnityEngine.UI.Text;
function Start () {
text = GetComponent(UnityEngine.UI.Text);
Counter = 0;
}
function Update () {
text.text = "Rocks: "+Counter;
}
function RockAdd () {
Counter++;
}
- Create -> UI -> Text (name it
EffText
)
- Add -> Script -> EffScript.js:
import UnityEngine.UI.Text;
private var Kills : int;
private var Rocks : int;
private var Eff : float;
private var text : UnityEngine.UI.Text;
function Start () {
text = GetComponent(UnityEngine.UI.Text);
Eff = 0;
Kills = 0;
Rocks = 0;
}
function Update () {
Kills = gameObject.Find("ScoreText").GetComponent(ScoreScript).Counter;
Rocks = gameObject.Find("RocksText").GetComponent(RocksScript).Counter;
if (Kills == 0 || Rocks == 0)
Eff = 0;
else
Eff = Kills * 1.0f / Rocks * 100;
text.text = "Eff: " + Eff.ToString("F0") + "%";
}