シューティングチュートリアルをUnity5で(第11回)

この回の操作が最大の難関だった。混乱した時に元に戻せるよう、Gitなどでのバージョン管理を推奨。SceneのSaveだとスクリプトは戻らないので不足。

11.1 HP(ヒットポイント)と攻撃力(power)の実装

  • 特記事項なし
using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {
    public int hp = 1;
    Spaceship spaceship;
    private Transform tran;

    void Start () {
        spaceship = GetComponent<Spaceship> ();
        tran = GetComponent<Transform> ();
        Move (tran.up * -1);

        StartCoroutine ("IE");
    }

    IEnumerator IE() {
        if (spaceship.canShot == false) {
            yield break;
        }

        while (true) {
            for (int i = 0; i < tran.childCount; i++) {
                Transform shotPosition = transform.GetChild (i);
                spaceship.Shot (shotPosition);
            }
            yield return new WaitForSeconds (spaceship.shotDelay);
        }
    }

    public void Move(Vector2 direction)
    {
        Rigidbody2D rb2 = GetComponent<Rigidbody2D> ();
        rb2.velocity = direction * spaceship.speed;
    }

    void OnTriggerEnter2D(Collider2D c)
    {
        string layerName = LayerMask.LayerToName (c.gameObject.layer);

        if (layerName != "Bullet(Player)")
            return;

        Destroy (c.gameObject);
        spaceship.Explosion ();
        Destroy (gameObject);
    }
}
  • 特記事項なし(チュートリアルだとlifeTImeの初期値が第5回の5から1に変更されているが、これだと敵の弾が空中で消えてしまうので変えない方が良い)
using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour {
    public int speed = 10;
    public float lifeTime = 5;
    public int power = 1; //追加箇所
    private Rigidbody2D rb2;

    void Start () {
        rb2 = GetComponent<Rigidbody2D>();
        rb2.velocity = transform.up * speed;
        Destroy (gameObject, lifeTime);
    }
}

ヒットポイントが0になった時に爆発させる

  • 特記事項なし
using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {
    public int hp = 1;
    Spaceship spaceship;
    private Transform tran;

    void Start () {
        spaceship = GetComponent<Spaceship> ();
        tran = GetComponent<Transform> ();
        Move (tran.up * -1);

        StartCoroutine ("IE");
    }

    IEnumerator IE() {
        if (spaceship.canShot == false) {
            yield break;
        }

        while (true) {
            for (int i = 0; i < tran.childCount; i++) {
                Transform shotPosition = transform.GetChild (i);
                spaceship.Shot (shotPosition);
            }
            yield return new WaitForSeconds (spaceship.shotDelay);
        }
    }

    public void Move(Vector2 direction)
    {
        Rigidbody2D rb2 = GetComponent<Rigidbody2D> ();
        rb2.velocity = direction * spaceship.speed;
    }

    void OnTriggerEnter2D(Collider2D c)
    {
        string layerName = LayerMask.LayerToName (c.gameObject.layer);

        if (layerName != "Bullet(Player)")
            return;

        Transform playerBulletTransform = c.transform.parent;
        Bullet bullet = playerBulletTransform.GetComponent<Bullet> ();

        hp = hp - bullet.power;

        Destroy (c.gameObject);

        if (hp <= 0) {
            spaceship.Explosion ();
            Destroy (gameObject);
        }
    }
}

アニメーター - レイヤーの作成

まずWindow>Animatorでウィンドウを開く。

レイヤーの追加

レイヤーの追加は右上の+ボタンをクリックする。

f:id:shady:20150524175532j:plain

レイヤー追加後のWeight設定は右上の歯車ボタンをクリックして別ウィンドウを開く。

f:id:shady:20150524175551j:plain

Damageアニメーションの作成

レイヤー追加すると初期状態でAny State, Entry, Exitという3つのStateがある。このチュートリアルを行うにあたっては気にしなくて良い。

Damageアニメーションの作成

チュートリアルだとNormalアニメーションが選択されているが代わりにEnemyが選択されていた。

f:id:shady:20150524175611j:plain

Add CurveボタンはAdd Propertyボタンになっている。

f:id:shady:20150524175628j:plain

DummyステートからDamageステートへのTransitionをクリックし、表示されたインスペクターのConditionsをDamageへと変更します。

Conditionsは最初空なので+ボタンで新規追加してDamageを追加する事になる。

DummyステートからDamageステートへのTransitionをクリックしてもInspectorにAtomic(タイムチャート)が表示されないことがある。

Atomicが表示されない。

f:id:shady:20150524175652j:plain

Atomicが表示される。

f:id:shady:20150524175721j:plain

表示されない時は選択しているレイヤーがBase Layerになっていないか確認する。テストの為にゲーム再生をすると、Animatorウィンドウ上では追加したレイヤーのステートマシンが表示されていても、レイヤー一覧上では自動的にBase Layerが選択されるからのようだ。

f:id:shady:20150524175810j:plain

Atomicはマウスホイール回転で拡大縮小、中ボタンクリックしながら左右移動で表示範囲を移動できる。