Shooting Game チュートリアル 第08回
すすめていきます。
8.1 BGMを付ける
なるほど、ちゃんとドロップしてもオブジェクトができない!と思ったらBackGroundにアタッチされていたことが発覚。
うしろのグレーのところにドロップでOK。
8.2 プレイヤーにショット音をつける
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float speed = 5;
public GameObject bullet;
Spaceship spaceship;
private AudioSource audiosource;
void Start() {
spaceship = GetComponent<Spaceship>();
audiosource = GetComponent<AudioSource>();
StartCoroutine ("FirstCoroutine");
}
IEnumerator FirstCoroutine ()
{
while (true) {
spaceship.Shot (transform);
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); //c == bullet etc,,
}
if (layerName == "Bullet(Enemy)" || layerName == "Enemy"){
spaceship.Explosion ();
Destroy (gameObject); //gameobject == player
}
}}
8.3 爆発音を付ける
なるほど、Explositonは生成と共に音がなるから、アタッチだけでいいんですね。
OK!
続きます。