Teachers open the door but You must enter by yourself.

Open Media Lab.オープンメディアラボ

スペースシューター
Space Shooter

「Space Shooter」公式チュートリアル


【事前学習】前回学んだ機能を再確認しておきましょう。

Game Setup, Player and Camera
Creating shots

球のゲームオブジェクトを作成し、プレハブ化します。

操作手順

  1. Create/Create Empty で空のオブジェクトを作成し名前を Bolt に変更。
  2. Bolt の子に Quad を作成し 名前を VFX に変更。
    Rotation.x:90。Mesh Collider を削除。
  3. Materials の中の fx_bolt_orange を VFX にドラッグ&ドロップ。
  4. Bolt に RigidBody を追加。Use Gravity のチェックは外す。
  5. Bolt に Capsule Collider を追加。Is Trigger にチェック。
    Radius:0.03, Height:0.5, Direction Z-Axis
  6. Bolt に Mover.csをアタッチ
    
    using UnityEngine;
    
    public class Mover : MonoBehaviour
    {
    	public float speed = 0.2f;
    	private Rigidbody rb;
    	
    	void Start(){
    		rb = GetComponent<Rigidbody>();
    		rb.velocity = transform.forward * speed;
    	}
    }
    
  7. Bolt を Asets フォルダにドラッグ&ドロップし Prefab 化。
  8. Hierarcy ウィンドウにある Bolt のオブジェクトは削除または Inspector のチェックを外して無効化。

Game Setup, Player and Camera
Shooting shots

球を発射する機能を追加します。

操作手順

  1. Player の子に空のオブジェクトを作成し、名前を Shot Spawn に変更。Transform の position.z:1.25。
  2. PlayerController.cs にショット用のコードを追加
    
    using UnityEngine;
    using UnityEngine.InputSystem;
    
    public class PlayerController : MonoBehaviour
    {
    	public InputAction move;
    	public InputAction fire;
    	public float speed = 0.3f;
    	public float tilt = 30.0f;
    
    	Rigidbody rb;
    	Vector3 movement;
    
    	public GameObject shot;
    	public Transform shotSpawn;
    	public float fireRate=0.25f;
    	float nextFire;
    
    	void Start(){
    		rb = GetComponent();
    	}
    
    	void OnMove(InputAction.CallbackContext c){
    		var movement2D= c.ReadValue<Vector2>();
    		movement=new Vector3(movement2D.x, 0f, movement2D.y);
    	}
    
    	void OnFire(InputAction.CallbackContext c){
    		if (Time.time > nextFire){
    			nextFire = Time.time + fireRate;
    			Instantiate(shot,shotSpawn.position,shotSpawn.rotation);
    		}
    	}
    
    	void FixedUpdate(){
            if(move.IsPressed()){
                rb.position += 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, movement.x * -tilt);
            }else{
                rb.rotation = Quaternion.identity;
            }
    	}
    
    	void Awake(){
    		move.performed+=OnMove;
    		fire.performed+=OnFire;
    	}
    
    	void OnEnable(){
    		move.Enable();
    		fire.Enable();
    	}
    }
  3. Player/PlayerController(Script) の Shot に Assets にある Bolt のプレハブをドラッグ&ドロップ。
  4. Player/PlayerController(Script) の ShotSpawnにPlayer/Shot Spawnをドラッグ&ドロップ。
  5. InputAction fire には Inspector でスペースキーやマウスの左ボタンなどをアタッチ。ActionTypeはButtonに設定します。

Boundaries, Hazards and Enemies
Boundary

操作手順

Bolt は発射される度に生成されますが消滅しません。当たったら消滅する境界を追加します。

  1. Boundary という名前で Cube を作成。Position(0,0,5)、Scale(15,1,30)。
  2. IsTrigger をチェックし、Mesh Renderer のチェックをはずす。
  3. Boundary にアタッチする DestroyByBoundary.cs
    
    using UnityEngine;
    
    public class DestroyByBoundary : MonoBehaviour
    {
    	private void OnTriggerExit(Collider other){
    		Destroy(other.gameObject);
    	}
    }
    

【事後学習】本日学んだ機能を再確認しておきましょう。

This site is powered by
Powered by MathJax