スペースシューター
Space Shooter
【事前学習】前回学んだ機能を再確認しておきましょう。
Game Setup, Player and Camera
Creating shots
操作手順
- Create/Create Empty で空のオブジェクトを作成し名前を Bolt に変更。
- Bolt の子に Quad を作成し 名前を VFX に変更。
Rotation.x:90。Mesh Collider を削除。 - Materials の中の fx_bolt_orange を VFX にドラッグ&ドロップ。
- Bolt に RigidBody を追加。Use Gravity のチェックは外す。
- Bolt に Capsule Collider を追加。Is Trigger にチェック。
Radius:0.03, Height:0.5, Direction Z-Axis - Bolt に Mover.csをアタッチ
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Mover : MonoBehaviour { public float speed = 20.0f; private Rigidbody rb; void Start(){ rb = GetComponent<Rigidbody>(); rb.velocity = transform.forward * speed; } }
- Bolt を Asets フォルダにドラッグ&ドロップし Prefab 化。
- Hierarcy ウィンドウにある Bolt のオブジェクトは削除または Inspector のチェックを外して無効化。
Game Setup, Player and Camera
Shooting shots
操作手順
- Player の子に空のオブジェクトを作成し、名前を Shot Spawn に変更。Transform の position.z:1.25。
- PlayerController.cs にショット用のコードを追加
public class PlayerController : MonoBehaviour { public float speed = 10.0f; public float tilt = 4.0f; private Rigidbody rb; public GameObject shot; public Transform shotSpawn; public float fireRate=0.25f; private float nextFire; void Start(){ rb = gameObject.AddComponent<Rigidbody>(); } private float moveHorizontal = 0f; private float moveVertical = 0f; void OnMove(InputValue movementValue){ Vector2 movementVector = movementValue.Get<Vector2>(); moveHorizontal = movementVector.x; moveVertical = movementVector.y; } void OnFire(){ if (Time.time > nextFire){ nextFire = Time.time + fireRate; Instantiate(shot,shotSpawn.position,shotSpawn.rotation); } } 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); } }
- Player/PlayerController(Script) の Shot に Assets にある Bolt のプレハブをドラッグ&ドロップ。
- Player/PlayerController(Script) の ShotSpawnにPlayer/Shot Spawnをドラッグ&ドロップ。
Boundaries, Hazards and Enemies
Boundary
操作手順
- Boundary という名前で Cube を作成。Position(0,0,5)、Scale(15,1,30)。
- IsTrigger をチェックし、Mesh Renderer のチェックをはずす。
- Boundary にアタッチする DestroyByBoundary.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DestroyByBoundary : MonoBehaviour { private void OnTriggerExit(Collider other){ Destroy(other.gameObject); } }
【事後学習】本日学んだ機能を再確認しておきましょう。