close

Portfolio


Unityで作った猫シューティング

※音が出ますのでご注意ください。
コードの一部を下記に載せます。(PCのみ)


ゲームマネージャー(C#)

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();
		}

		// 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;
		}
}
			
↑TOP↑

宇宙猫シップ(C#)

				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();
	}

	// 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);
			}
	}
}

↑TOP↑

敵生成(C#)

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().SetGameManager(gm);
						yield return new WaitForSeconds(delta);
						if(delta > 0.5f){
								delta -= 0.05f;
						}
				}
		}

}
				
↑TOP↑

弾の廃棄(C#)

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)
						);
				}
		}
}
				
↑TOP↑

noteの記事はこちら
チーム開発実習で作ったミニゲーム集

チーム開発実習(Unity)

Unityで作ったゲーム集

ゲーム集(Unity)

Javaで作ったねこあるきゲーム

アドベンチャー(Java)

QRコード

note

↑TOP↑