Last active
August 29, 2015 14:06
-
-
Save MrSmart00/b93159946a368189ac13 to your computer and use it in GitHub Desktop.
Unity4.6オープンβでチュートリアルのShoothing Game作ってみる ref: http://qiita.com/MrSmart/items/b7e4eeebd1435fe8861b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Enemy : Spaceship { | |
// ヒットポイント | |
public int hp = 1; | |
// 弾を撃つかどうか | |
public bool canShot; | |
// スコアのポイント | |
public int point = 100; | |
IEnumerator Start () | |
{ | |
// ローカル座標のY軸のマイナス方向に移動する | |
Move (transform.up * -1); | |
// canShotがfalseの場合、ここでコルーチンを終了させる | |
if (canShot == false) { | |
yield break; | |
} | |
while (true) { | |
// 子要素を全て取得する | |
for (int i = 0; i < transform.childCount; i++) { | |
Transform shotPosition = transform.GetChild (i); | |
// ShotPositionの位置/角度で弾を撃つ | |
Shot (shotPosition); | |
} | |
// shotDelay秒待つ | |
yield return new WaitForSeconds (shotDelay); | |
} | |
} | |
// 機体の移動 | |
protected override void Move (Vector2 direction) | |
{ | |
rigidbody2D.velocity = direction * speed; | |
} | |
void OnTriggerEnter2D (Collider2D c) | |
{ | |
// レイヤー名を取得 | |
string layerName = LayerMask.LayerToName (c.gameObject.layer); | |
// レイヤー名がBullet (Player)以外の時は何も行わない | |
if (layerName != "Bullet (Player)") return; | |
// PlayerBulletのTransformを取得 | |
Transform playerBulletTransform = c.transform.parent; | |
// Bulletコンポーネントを取得 | |
Bullet bullet = playerBulletTransform.GetComponent<Bullet>(); | |
// ヒットポイントを減らす | |
hp = hp - bullet.power; | |
// 弾の削除 | |
Destroy(c.gameObject); | |
// ヒットポイントが0以下であれば | |
if(hp <= 0 ) | |
{ | |
// スコアコンポーネントを取得してポイントを追加 | |
FindObjectOfType<Score>().AddPoint(point); | |
// 爆発 | |
Explosion (); | |
// エネミーの削除 | |
Destroy (gameObject); | |
}else{ | |
GetComponent<Animator> ().SetTrigger("Damage"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create > UI > Text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create > UI > Text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create > UI > Canvas | |
//作ったCanvasを選択しつつ | |
create > UI > Text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void GameOver () | |
{ | |
// ハイスコアの保存 | |
FindObjectOfType<Score>().Save(); | |
// ゲームオーバー時に、タイトルを表示する | |
title.enabled = true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Player : Spaceship { | |
IEnumerator Start () | |
{ | |
while (true) { | |
// 弾をプレイヤーと同じ位置/角度で作成 | |
Shot (transform); | |
// ショット音を鳴らす | |
audio.Play(); | |
// shotDelay秒待つ | |
yield return new WaitForSeconds (shotDelay); | |
} | |
} | |
void Update () | |
{ | |
// 右・左 | |
float x = Input.GetAxisRaw ("Horizontal"); | |
// 上・下 | |
float y = Input.GetAxisRaw ("Vertical"); | |
// 移動する向きを求める | |
Vector2 direction = new Vector2 (x, y).normalized; | |
// 移動の制限 | |
Move (direction); | |
} | |
// 機体の移動 | |
protected override void Move (Vector2 direction) | |
{ | |
// 画面左下のワールド座標をビューポートから取得 | |
Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); | |
// 画面右上のワールド座標をビューポートから取得 | |
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); | |
// プレイヤーの座標を取得 | |
Vector2 pos = transform.position; | |
// 移動量を加える | |
pos += direction * speed * Time.deltaTime; | |
// プレイヤーの位置が画面内に収まるように制限をかける | |
pos.x = Mathf.Clamp (pos.x, min.x, max.x); | |
pos.y = Mathf.Clamp (pos.y, min.y, max.y); | |
// 制限をかけた値をプレイヤーの位置とする | |
transform.position = pos; | |
} | |
// ぶつかった瞬間に呼び出される | |
void OnTriggerEnter2D (Collider2D c) | |
{ | |
// レイヤー名を取得 | |
string layerName = LayerMask.LayerToName(c.gameObject.layer); | |
// レイヤー名がBullet (Enemy)の時は弾を削除 | |
if( layerName == "Bullet (Enemy)") | |
{ | |
// 弾の削除 | |
Destroy(c.gameObject); | |
} | |
// レイヤー名がBullet (Enemy)またはEnemyの場合は爆発 | |
if( layerName == "Bullet (Enemy)" || layerName == "Enemy") | |
{ | |
// Managerコンポーネントをシーン内から探して取得し、GameOverメソッドを呼び出す | |
FindObjectOfType<Manager>().GameOver(); | |
// 爆発する | |
Explosion(); | |
// プレイヤーを削除 | |
Destroy (gameObject); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections; | |
public class Score : MonoBehaviour { | |
// スコアを表示するGUIText | |
public Text scoreGUIText; | |
// ハイスコアを表示するGUIText | |
public Text highScoreGUIText; | |
// スコア | |
private int score; | |
// ハイスコア | |
private int highScore; | |
// PlayerPrefsで保存するためのキー | |
private string highScoreKey = "highScore"; | |
void Start () | |
{ | |
Initialize (); | |
} | |
void Update () | |
{ | |
// スコアがハイスコアより大きければ | |
if (highScore < score) { | |
highScore = score; | |
} | |
// スコア・ハイスコアを表示する | |
scoreGUIText.text = score.ToString (); | |
highScoreGUIText.text = "HighScore : " + highScore.ToString (); | |
} | |
// ゲーム開始前の状態に戻す | |
private void Initialize () | |
{ | |
// スコアを0に戻す | |
score = 0; | |
// ハイスコアを取得する。保存されてなければ0を取得する。 | |
highScore = PlayerPrefs.GetInt (highScoreKey, 0); | |
} | |
// ポイントの追加 | |
public void AddPoint (int point) | |
{ | |
score = score + point; | |
} | |
// ハイスコアの保存 | |
public void Save () | |
{ | |
// ハイスコアを保存する | |
PlayerPrefs.SetInt (highScoreKey, highScore); | |
PlayerPrefs.Save (); | |
// ゲーム開始前の状態に戻す | |
Initialize (); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
// Rigidbody2Dコンポーネントを必須にする | |
[RequireComponent(typeof(Rigidbody2D))] | |
public abstract class Spaceship : MonoBehaviour | |
{ | |
// 移動スピード | |
public float speed; | |
// 弾を撃つ間隔 | |
public float shotDelay; | |
// 弾のPrefab | |
public GameObject bullet; | |
// 爆発のPrefab | |
public GameObject explosion; | |
// 爆発の作成 | |
public void Explosion () | |
{ | |
Instantiate (explosion, transform.position, transform.rotation); | |
} | |
// 弾の作成 | |
public void Shot (Transform origin) | |
{ | |
Instantiate (bullet, origin.position, origin.rotation); | |
} | |
protected abstract void Move (Vector2 direction); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment