スペースシューター
Space Shooter
【事前学習】前回学んだ機能を再確認しておきましょう。
Scoring, Finishing and Building the Game
Audio
操作手順
- Asteroid の爆発音の追加は Prefabs/VFX/explosion_asteroid(プレハブ)を OpenPrefab ボタンを押して展開。Hierarcy ウィンドウに eplosion_asteroid(プレハブ)の構成要素が表示されるので、そこに Audio/eplosion_asteroid(音オブジェクト)をドラッグ&ドロップ。
- Player の爆発音の追加も同様。
- BGM の追加は Audio/music_background を Hierarcy ウィンドウのGameController にドラッグ&ドロップ。Loop にチェックをつける。
- ミサイルの発射音は Audio/weapon_player を Hierarcy ウィンドウのPlayer にドラッグ&ドロップ。PlayOnAwake のチェックをはずし、PlayerController.csに以下のように追記。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class PlayerController : MonoBehaviour { public float speed=10.0f; public float tilt = 4.0f; private Rigidbody rb; private AudioSource a; public GameObject shot; public Transform shotSpawn; public float fireRate=0.25f; private float nextFire; void Start(){ rb = GetComponent<Rigidbody>(); a = GetComponent<AudioSource>(); } private float moveHorizontal = 0f; private float moveVertical = 0f; void OnMove(InputValue input) { var vector2d = input.Get
(); moveHorizontal = vector2d.x; moveVertical = vector2d.y; } void OnFire(){ if (Time.time > nextFire){ nextFire = Time.time + fireRate; Instantiate(shot,shotSpawn.position,shotSpawn.rotation); a.Play(); } } void FixedUpdate(){ Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); rb.velocity = movement * speed; rb.position = new Vector3( Mathf.Clamp(rb.position.x, -6, 6), 0.0f, Mathf.Clamp(rb.position.z, -4, 8) ); rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt); } }
【事後学習】本日学んだ機能を再確認しておきましょう。