見出し画像

ゲームを作る課題(Unity) 〜宇宙ねこシューティング〜

Unityで作ったねこがプレイヤーのシューティングゲームです。

それまでの応用としてゲームを作るという課題でした。

(ちなみにUnity基礎の授業は体調不良で受けておらず)

3Dなのに2D感のあるゲームを作りたくて、授業でも弾を飛ばすスクリプトを書いたりしていたので、迷わず縦型シューティングを作ることにしました。

昔、宇宙猫Tシャツって流行ったなぁ、なんて思いながらこういった画像のチョイスとなりました。

曲やSEについて

曲がギャラクティカル(?)で気に入っています。

ねこの声も可愛くイメージにぴったりでした。

挙動はあやしい……

しかし、エネミーの見た目もそうですが、動きも怪しく

ゲームオーバーはエネミーが床に着いたらなのですが、ゲームオーバーになってもねこは動けます。

しかも、ねこが画面外に行ってしまうと2度と帰ってこれません。

まだまだまだ勉強不足が目立つ制作物のひとつでした。

GameManager .cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
   public GUIStyle scoreStyle;
   public GUIStyle msgStyle;
   private int score = 0;
   private string msg = "";

   private AudioBgmStop audioBgmStop;

   AudioSource gameOverSound;

   void Start(){
       gameOverSound = GetComponent<AudioSource>();
   }

   // Update is called once per frame
   void Update()
   {
       if(msg == "GameOver"){
			Time.timeScale = 0f;
			gameOverSound.Play();
           //audioBgmStop.Stop();
       }
   }

   void OnGUI(){
       GUI.Label(new Rect(70, 40, 80, 20), score.ToString(), scoreStyle);
       GUI.Label(new Rect(Screen.width/2-250, Screen.height/2-25, 300, 50),msg, msgStyle);
   }
   public int GetScore(){
       return score;
   }
   public void SetScore(int socre){
       this.score = socre;
   }
   public string GetMsg(){
       return msg;
   }
   public void SetMsg(string msg){
       this.msg = msg;
   }
}

ShipScript.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShipScript : MonoBehaviour
{
   AudioSource spaceShotSound;
   public GameObject prefab;
   Vector3 vec; //出荷しただけでnullではなく0,0,0が入ってる(構造体) クラスやフィールドに似てるけど構造体は参照型じゃない
   public float speed;

   void Start(){
       spaceShotSound = GetComponent<AudioSource>();
   }

   // Update is called once per frame
   void Update()
   {
       vec.x = Input.GetAxis("Horizontal"); //左右の移動
       transform.Rotate(Input.GetAxis("Vertical"), 0, 0); //上下を押したときの回転(x回転) 上を押すと前に倒れる
       transform.Translate(vec * speed); //x軸方向に動く
       if(Input.GetButtonDown("Jump")){ //スペースボタンを押したときに
           GameObject bullet = Instantiate(
               prefab, //何を
               transform.position, //どこに
               Quaternion.LookRotation(Quaternion.Euler(-90f, 0, 0) * transform.forward) //どの角度で
               //Quaternionを掛け算で更に回せる オイラー角を使う
               //LookRotationは引数にベクトルを与えることでそのベクトルを向くQuaternion(回転)を作ってくれる
               //transform.forward はこの物体がアタッチされているz軸の向き(この場合Ship)
               //transform.rotation にすると腹から出る(だから90度ずらしてる)
           );
           spaceShotSound.Play();
           Destroy(bullet, 2f);
       }
   }
}

EneGenerator.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EneGenerator : MonoBehaviour
{
   public GameObject prefab;
   public GameManager gm;

   // Start is called before the first frame update
   void Start()
   {
       StartCoroutine(Create());
   }

   IEnumerator Create(){
       float delta = 3.0f;
       while(true){
           GameObject obj = Instantiate(
               prefab,
               new Vector3(Random.Range(-12.0f,12.0f),Random.Range(40.0f,70.0f),3.11f),
               Quaternion.identity
           );
           obj.GetComponent<EneController>().SetGameManager(gm);
           yield return new WaitForSeconds(delta);
           if(delta > 0.5f){
               delta -= 0.05f;
           }
       }
   }

}

BulletDestroyer.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletDestroyer : MonoBehaviour
{
   public GameObject bulletPrefab;
   public Vector3 bulletRotation;

   void OnTriggerEnter(Collider other){
       if(other.gameObject.tag == "SpaceBallet"){
           Destroy(other.gameObject);
       }

       if(bulletPrefab != null){
           Instantiate(
               bulletPrefab,
               other.transform.position,
               Quaternion.Euler(bulletRotation)
           );
       }
   }
}

良ければサポートよろしくお願いします!いただいたサポートは愛猫姉妹のごはんとおやつ代に使わせていただきます♡♡