シューティングチュートリアルをUnity5で(第06〜08回)
第06回
6.2 Frontを作る
Quadを作成すると黒に近い灰色になっているのでそこにbackground-frontを乗せるとチュートリアルページの様な青い背景にならず、Playすると暗い画面になった。
Front、Middle、BackのShaderをそれぞれStandardからUnlit/Transparentに変更すると真っ暗ではなくなる。
6.5 背景をスクロールさせる
- Rendererを前もって取得しておく
using UnityEngine; using System.Collections; public class Background : MonoBehaviour { public float speed = 0.1f; private Renderer ren; void Start () { ren = GetComponent<Renderer> (); } // Update is called once per frame void Update () { float y = Mathf.Repeat (Time.time * speed, 1); Vector2 offset = new Vector2 (0, y); ren.sharedMaterial.SetTextureOffset ("_MainTex", offset); } }
第07回
7.2 Waveを呼び出すEmitter (エミッター)を作成する
- CoroutineをStartメソッドから呼び出す。
using UnityEngine; using System.Collections; public class Emitter : MonoBehaviour { public GameObject[] waves; private int currentWave; void Start () { StartCoroutine ("IE"); } IEnumerator IE() { if (waves.Length == 0) { yield break; } GameObject wave = (GameObject)Instantiate (waves [currentWave], transform.position, Quaternion.identity); wave.transform.parent = transform; while (wave.transform.childCount != 0) { yield return new WaitForEndOfFrame (); } Destroy (wave); if (waves.Length <= ++currentWave) { currentWave = 0; } } }
第08回
8.1 BGMを付ける
Sounds/BGMにあるbgmを選択し、インスペクターの3D Soundのチェックが外れていることを確認して下さい。
- bgm Import Settingsに3D Soundが存在しない。
bgmファイルをシーンビューにドロップする時は背景画像上にドロップするとBackgrounds>Frontに追加されてしまうのでチュートリアル通り灰色の部分にドロップする。
8.2 プレイヤーにショット音をつける
- AudioSourceを前もって取得しておく。
using UnityEngine; using System.Collections; public class Player : MonoBehaviour { Spaceship spaceship; private Transform tran; private AudioSource audioSource; void Start() { spaceship = GetComponent<Spaceship>(); tran = GetComponent<Transform>(); audioSource = GetComponent<AudioSource> (); StartCoroutine("IE"); } IEnumerator IE() { while (true) { spaceship.Shot(tran); audioSource.Play (); yield return new WaitForSeconds(spaceship.shotDelay); } } void Update () { float x = Input.GetAxisRaw("Horizontal"); float y = Input.GetAxisRaw("Vertical"); Vector2 direction = new Vector2(x, y).normalized; spaceship.Move(direction); } void OnTriggerEnter2D(Collider2D c) { string layerName = LayerMask.LayerToName(c.gameObject.layer); if (layerName == "Bullet(Enemy)") { Destroy (c.gameObject); } if (layerName == "Bullet(Enemy)" || layerName == "Enemy") { spaceship.Explosion (); Destroy (gameObject); } } }