close

Portfolio


Unityで作ったゲーム集

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

ボタンセレクト(C#)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Linq;

public class ButtonSelectTitle : MonoBehaviour
{
	string[] selectGameTitle = { //遷移するシーン名を初期値として配列に
				"", //番号を合わせるための空要素
				"NovelGameTitle", //ノベルゲーム予定
				"CoinGameTitle", //コインを取得するゲーム
				"GiftyChan_Title"  //お金を交番に届けるゲーム
		};

	static int[] selectGameFlag = { 0, 0, 0, 0 }; //クリア判定のための配列(最初の要素は番号を合わせるための空要素)

	int numbers;
	float step_time;

	void Start()
	{
		// Debug.Log("Start");
		// Debug.Log(selectGameFlag.Sum());
		numbers = 0;
		step_time = 0.0f; //経過時間初期化

		if (selectGameFlag.Sum() >= 3)
		{ //それぞれのゲーム全てからタイトルに戻ってきたら
			Debug.Log("ALL GamePlaying!!!");
			for (int i = 0; i < selectGameFlag.Length; i++)
			{ //初期値を0にリセット
				selectGameFlag[i] = 0;
			}
			SceneManager.LoadScene("AllPlaying"); //クリアシーンへ遷移
		}
	}

	void Update()
	{
		if (numbers>0)
		{
			Debug.Log("Update:numbers" + numbers);
			//経過時間をカウント
			step_time += Time.deltaTime;
			if (step_time >= 1.0f)
			{
				SceneManager.LoadScene(selectGameTitle[numbers]);
			}
		}
	}

	//Buttonにnumberを割り振ってnumberを元にselectGameTitleのインデックスとして各ゲームシーンに遷移
	public void OnClick(int number)
	{
		// Debug.Log(number);
		if(numbers==0){
			if (selectGameTitle[number] != "")
			{
				selectGameFlag[number] = 1;
				numbers = number;
			}
		}
	}
}
						
		
↑TOP↑

-Coinゲーム-

コイン生成(C#)

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

public class Coin : MonoBehaviour
{
	public const int coinsNums=300;
	public GameObject coinPrefab;
	public float speed;
	//public static Renderer targetRenderer;

	// Start is called before the first frame update
	void Start()
		{
		for (int i = 0; i < coinsNums;i++){
			createCoin();
		}
		//targetRenderer = GetComponent();
	}
		void Update(){

		}

		void createCoin(){
		GameObject coin = Instantiate(coinPrefab) as GameObject;
		coin.transform.position = new Vector2(Random.Range(8.0f, 80.0f), Random.Range(-2.0f, 3.9f));
	}
}

		
↑TOP↑

Unityちゃんを動かす(C#)

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

public class UnityChController : MonoBehaviour
{
	Rigidbody2D rb2d;
	Animator animator;
	float angle;
	bool isDead;

	public float maxHeight;
	public float swimVelocity;
	public float relativeVelocityX;
	public GameObject sprite;

	public bool IsDead(){
		return isDead;
	}

	void Awake(){
		rb2d = GetComponent();
		animator = sprite.GetComponent();
		//アニメーターのswimフラグをtrueにする
		animator.SetBool("swim", true);
	}

		// Update is called once per frame
		void Update()
		{
			if((Input.GetButtonDown("Jump") && transform.position.y < maxHeight) || (Input.GetButtonDown("Fire1") && transform.position.y < maxHeight)){ //入力の判定
				Swim();
			}
			// if(Input.GetButtonDown("Fire1") && transform.position.y < maxHeight){
			// 	Swim();
			// }

		//角度を反映
		//ApplyAngle();
	}

	public void Swim(){
		//死んだら泳げない
		if(isDead){
			return;
		}

		if(rb2d.isKinematic){
			return;
		}

		rb2d.velocity = new Vector2(0.0f, swimVelocity); //速度の操作
	}

		/*
		void ApplyAngle(){
		//現在の速度、相対速度から進んでいる角度を求める
		float targetAngle = Mathf.Atan2(rb2d.velocity.y, relativeVelocityX) * Mathf.Rad2Deg; //ベクトルから角度の計算

		//回転アニメをスムージング
		angle = Mathf.Lerp(angle, -targetAngle, Time.deltaTime * 10.0f);

		//Rotationの反映
		sprite.transform.localRotation = Quaternion.Euler(0.0f, 0.0f, angle);
	}
		*/

	void OnCollisionEnter2D(Collision2D collision){ //コリジョンによる死亡判定
		if(isDead){
			return;
		}
		//何かにぶつかったら死亡フラグを立てる
		isDead = true;
		animator.SetBool("swim", false);
		//Debug.Log("swim:false");
	}

	public void SetSteerActive(bool active){
		rb2d.isKinematic = !active;
	}
}
		
↑TOP↑

-Runゲーム-

ゲームコントローラー(C#)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GiftyGameController : MonoBehaviour
{
	PlayerRun2D Unitychan2DRun;
	PlayerJump2D Unitychan2DJump;
	public GameObject unitychan;
	public GameObject startUnitychan;
	public GameObject clearUnitychan;
	public Text moneyLabel;
	public Text timeLabel;
	public Text stateLabel;

	public DateTime startDateTime; //スタート時のDateTime構造体
	public TimeSpan totalTime; //時間経過のTimeSpan構造体

	public DateTime readyStartDateTime; //Ready用
	public TimeSpan readyTotalTime; //Ready用
	public static int oldCountDown; //CountDown用
	public static bool isClear; //ClearPointにきてるか判定するために
	public static TimeSpan[] scoreTimes = { new TimeSpan(0, 59, 59), new TimeSpan(0, 59, 59), new TimeSpan(0, 59, 59), new TimeSpan(0, 59, 59) }; //時間のハイスコア用
	public static int[] scoreMoneys = { 0, 0, 0, 0 }; //取得金額のハイスコア用

	//List ghost=new List>(); //ゴースト用のlist
	static int f; //フレームのカウント

	GameObject Voice;
	AudioSource startVoice;

	GameObject bgm;
	AudioSource gameBGM;

	enum State
	{
		Ready,
		Play,
		Clear
	}

	State state;

	// Start is called before the first frame update
	void Start()
	{
		Debug.Log("Strat() --------------------------------");
	}

	void Awake()
	{
		Voice = GameObject.FindGameObjectWithTag("StartVoice");
		startVoice=Voice.GetComponent();
		bgm = GameObject.FindGameObjectWithTag("BGM");
		gameBGM = bgm.GetComponent();
		Debug.Log("Awake()");
		Ready();
	}

	// Update is called once per frame
	void Update()
	{
		Debug.Log("state:" + state);
		switch (state)
		{
			case State.Ready:
				//現在のDateTimeから初期のDateTimeを計算し経過した時間を計算する
				readyTotalTime = DateTime.Now - readyStartDateTime;
				int countDown = (3 - readyTotalTime.Seconds);
				if (countDown != oldCountDown)
				{
					oldCountDown = countDown;
					//TimeSpan構造体のプロパティで秒をテキストにいれる
					stateLabel.text = countDown.ToString("0");
				}
				if (countDown == 0)
				{
					stateLabel.text = "届けて!!\nUnityちゃん!!";
					Debug.Log(stateLabel.text);
					GameStart();
				}
				break;
			case State.Play:
				//現在のDateTimeから初期のDateTimeを計算し経過した時間を計算する
				totalTime = DateTime.Now - startDateTime;
				//TimeSpan構造体のプロパティで分と秒をテキストにいれる
				timeLabel.text = totalTime.Minutes.ToString("00") + ":" + totalTime.Seconds.ToString("00") + "." + totalTime.Milliseconds.ToString("000");
				if (isClear == true)
				{
					Clear();
				}
				break;
			case State.Clear:
				if (Input.GetButtonDown("Fire1"))
				{
					Reload();
				}
				else{
					Invoke("Reload", 5f);
				}
				break;
		}
		moneyLabel.text = string.Format("{0:#,0}円", Money.moneyCount);
		//GhostRecording();
	}

	void Ready()
	{
		Debug.Log("Ready()");
		state = State.Ready;
		isClear = false;
		startUnitychan.SetActive(true);
		unitychan.SetActive(false);
		clearUnitychan.SetActive(false);

		Money.moneyCount = 0;
		moneyLabel.text = string.Format("{0:#,0}円", 0);
		timeLabel.text = "00:00.000";
		readyStartDateTime = DateTime.Now;
		oldCountDown = 0;
	}

	void GameStart()
	{
		startVoice.Play();
		gameBGM.time = 10f;
		Debug.Log("GameStart()");
		state = State.Play;
		Destroy(startUnitychan);
		unitychan.SetActive(true);
		Invoke("ClearStateLabel", 0.8f);

		//現在のDateTime構造体を設定
		startDateTime = DateTime.Now;
	}

	void Clear()
	{
		state = State.Clear;

		HiscoreMoneySort();
		HiscoreTimeSort();

		unitychan.SetActive(false);
		clearUnitychan.SetActive(true);
		gameBGM.Stop();
		stateLabel.gameObject.SetActive(true);
		stateLabel.text = "お金を\n" + string.Format("{0:#,0}円", Money.moneyCount) + " 拾って\n交番に届けたよ!!!";


		//Time.timeScale = Mathf.Approximately(Time.timeScale, 0f) ? 1f : 0f;

		//scoreMoney = Money.moneyCount;
		//SaveScoreMoney();
	}

	void Reload()
	{
		SceneManager.LoadScene("GiftyChan_Title");
	}

	public static void IsClear()
	{
		isClear = true;
	}

	void ClearStateLabel()
	{
		stateLabel.gameObject.SetActive(false);
		stateLabel.text = "";
	}

	void HiscoreMoneySort()
	{
		scoreMoneys[3] = Money.moneyCount;
		Array.Sort(scoreMoneys);
		Array.Reverse(scoreMoneys);
	}

	void HiscoreTimeSort()
	{
		scoreTimes[3] = totalTime;
		Array.Sort(scoreTimes);
	}

	public static int[] GetMoneyHiscore()
	{
		int[] gmh = new int[3];
		for (int i = 0; i < 3; i++)
		{
			gmh[i] = scoreMoneys[i];
		}
		return gmh;
	}

	public static TimeSpan[] GetTimeHiscore()
	{
		TimeSpan[] gth = new TimeSpan[3];
		for (int i = 0; i < 3; i++)
		{
			gth[i] = scoreTimes[i];
		}
		return gth;
	}


	void GhostRecording()
	{
		bool[] addGhost = new bool[3];

		//入力を受け取ってデータ化
		if (Input.GetKeyDown(KeyCode.Space))
		{
			addGhost[0] = true;
		}
		else
		{
			addGhost[0] = false;
		}

		if (Input.GetKeyDown(KeyCode.LeftArrow))
		{
			addGhost[1] = true;
		}
		else
		{
			addGhost[1] = false;
		}

		if (Input.GetKeyDown(KeyCode.RightArrow))
		{
			addGhost[2] = true;
		}
		else
		{
			addGhost[2] = false;
		}
		//ghost.Add(addGhost);
	}

}
				
		
↑TOP↑

noteの記事はこちら

QRコード

note

チーム開発実習で作ったミニゲーム集

チーム開発実習(Unity)

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

シューティング(Unity)

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

アドベンチャー(Java)

96nekoBase

coconala

↑TOP↑