BallGame

(アプリ紹介・Unityサンプルコード)

2016-09-04 01.12.26

・iPhone専用アプリ(app storeから)
・ソフトウェア;Unity5.3.6
・プログラミング言語;C#

Unityで作成したシンプルなボール転がしゲームです。このゲームのサンプルコードはこのサイトにて随時公開していきます。Unity及びプログラミング学習用に活用してみてください。


1)ボールを動かす/ボールでコインをとる

2016-09-04 01.12.30

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Ball : MonoBehaviour {

	private float moveSpeed = 8; // スマホでの速度は「8」
	public AudioClip coinGet;
	public AudioClip stairsSound;
	public GameObject stairs;
	public int clearCount;
	public string floorNumber;
	private Rigidbody rb;
	private int coinCount;

	void Start () {

		rb = GetComponent<Rigidbody>();
		coinCount = 0;
	
	}

	void Update () {

		Vector3 vector = new Vector3();

		if(Application.isEditor){
			vector.x = Input.GetAxis("Horizontal");
			vector.z = Input.GetAxis("Vertical"); 
			Vector3 movement = new Vector3(vector.x, 0.0f, vector.z);
			rb.AddForce(movement * moveSpeed);
		} else {
			vector.x = Input.acceleration.x;
			vector.z = Input.acceleration.y;  // ここがポイント
			Vector3 movement = new Vector3(vector.x, 0.0f, vector.z);
			rb.AddForce(movement * moveSpeed);
		}
	}
		
	void OnTriggerEnter(Collider other){
		if(other.gameObject.CompareTag("Coin")){
			Destroy(other.gameObject);
			AudioSource.PlayClipAtPoint(coinGet, transform.position);

			coinCount += 1;

			if(coinCount == clearCount){
				AudioSource.PlayClipAtPoint(stairsSound, transform.position);
				stairs.SetActive(true);
			}
		} else if(other.gameObject.CompareTag("EnemyShell")){
			Destroy(other.gameObject);
			SceneManager.LoadScene(floorNumber + "F");
		}
	}
}

2)ボールをワープさせる(ワープポイント)

2016-09-04 01.12.55

using UnityEngine;
using System.Collections;

public class WarpPoint : MonoBehaviour {

	public Vector3 pos;

	void OnTriggerEnter(Collider other){
		if(other.gameObject.CompareTag("Player")){
			other.gameObject.transform.position = new Vector3(pos.x, pos.y, pos.z);
		}
	}
}

3)ボールを加速させる(加速装置)

2016-09-05 04.39.53

using UnityEngine;
using System.Collections;

public class AccelPoint : MonoBehaviour {

	public Vector3 pos;

	void OnTriggerEnter(Collider other){
		other.gameObject.GetComponent<Rigidbody>().AddForce(new Vector3(pos.x, pos.y, pos.z), ForceMode.VelocityChange);
	}
}

4)ボールの大きさを変える

2016-09-04 01.13.22

using UnityEngine;
using System.Collections;

public class ScaleChange : MonoBehaviour {

	public Vector3 pos;

	void OnTriggerEnter(Collider other){
		if(other.gameObject.CompareTag("Player")){
			other.gameObject.transform.localScale = new Vector3(pos.x, pos.y, pos.z);
		}
	}
}

5)落ちる床
2016-09-04 01.14.00

using UnityEngine;
using System.Collections;

public class FallBlock : MonoBehaviour {

	private Rigidbody rb;

	void Start(){
		rb = GetComponent<Rigidbody>();
	}

	void OnCollisionEnter(Collision other){
		if(other.gameObject.CompareTag("Player")){
			Invoke("Fall", 1);
		}
	}

	void Fall(){
		rb.isKinematic = false;
	}
}

6)動く床

using UnityEngine;
using System.Collections;

public class MoveBlock : MonoBehaviour {

	private Vector3 pos;

	void Start () {
		pos = transform.position;
	}

	void Update () {

		transform.position = new Vector3(pos.x, pos.y,  Mathf.Sin(Time.time) * 2 + pos.z);
	
	}
}

7)追いかけてくる敵

2016-09-05 04.41.25

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class ChaseEnemy : MonoBehaviour {

	public GameObject target;
	public string floorNumber;
	private NavMeshAgent agent;


	void Start () {
		agent = GetComponent<NavMeshAgent>();
	}

	void Update () {

		agent.destination = target.transform.position;
	}

	void OnCollisionEnter(Collision other){
		if(other.gameObject.CompareTag("Player")){
			SceneManager.LoadScene(floorNumber + "F");
		}
	}
}

8)弾を発射する敵

2016-09-05 04.44.26

using UnityEngine;
using System.Collections;

public class EnemyShot : MonoBehaviour {

	public GameObject shellPrefab;
	public float shotSpeed;
	public AudioClip shotSound;
	private int timeCount = 0;


	void Update () {

		timeCount += 1;

		if(timeCount % 70 == 0){
			GameObject enemyShell = Instantiate(shellPrefab, transform.position, Quaternion.identity) as GameObject;
			Rigidbody enemyShellRb = enemyShell.GetComponent<Rigidbody>();
			enemyShellRb.AddForce(transform.forward * shotSpeed);
			AudioSource.PlayClipAtPoint(shotSound, transform.position);
			Destroy(enemyShell, 2.0f);
		}
	}
}

9)タイムアタックを作る(UI)

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-09-09-6-13-47

using UnityEngine;
using System.Collections;

// ★タイムアタック
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour {

	// ★タイムアタック
	public float timeCount;
	public Text timeLabel;
	public GameObject timeOverLabel;
	public string floorNumber;


	public Ball coinCount;
	public CoinPanel coinPanel;
	private GameObject ball;


	void Update () {

		coinPanel.Updatecoin(coinCount.Coin());

		// ★タイムアタック
		timeCount -= Time.deltaTime;
		timeLabel.text = "TIME: " + timeCount.ToString("0");

		if(timeCount < 0){
			ball = GameObject.Find("Ball");
			Ball ballScript = ball.GetComponent<Ball>();
			ballScript.enabled = false;

			timeOverLabel.SetActive(true);
			Invoke("Retry", 0.6f);
		}
	}

	// ★タイムアタック
	void Retry(){
		SceneManager.LoadScene(floorNumber + "F");
	}
}

10)コインパネルを作る(UI)

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-09-09-6-21-56

①GameController.cs

using UnityEngine;
using System.Collections;

// ★コインパネル
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour {

	public float timeCount;
	public Text timeLabel;
	public GameObject timeOverLabel;
	public string floorNumber;

	// ★コインパネル
	public Ball coinCount;
	public CoinPanel coinPanel;
	private GameObject ball;


	void Update () {

		// ★コインパネル
		coinPanel.Updatecoin(coinCount.Coin());


		timeCount -= Time.deltaTime;
		timeLabel.text = "TIME: " + timeCount.ToString("0");

		if(timeCount < 0){
			ball = GameObject.Find("Ball");
			Ball ballScript = ball.GetComponent<Ball>();
			ballScript.enabled = false;

			timeOverLabel.SetActive(true);
			Invoke("Retry", 0.6f);
		}
	}
		
	void Retry(){
		SceneManager.LoadScene(floorNumber + "F");
	}
}

②Ball.cs

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Ball : MonoBehaviour {

	private float moveSpeed = 8; // スマホでの速度は「8」
	public AudioClip coinGet;
	public AudioClip stairsSound;
	public GameObject stairs;
	public int clearCount;
	public string floorNumber;
	private Rigidbody rb;
	private int coinCount;

	void Start () {

		rb = GetComponent<Rigidbody>();
		coinCount = 0;
	
	}

	void Update () {

		Vector3 vector = new Vector3();

		if(Application.isEditor){
			vector.x = Input.GetAxis("Horizontal");
			vector.z = Input.GetAxis("Vertical"); 
			Vector3 movement = new Vector3(vector.x, 0.0f, vector.z);
			rb.AddForce(movement * moveSpeed);
		} else {
			vector.x = Input.acceleration.x;
			vector.z = Input.acceleration.y;  // ここがポイント
			Vector3 movement = new Vector3(vector.x, 0.0f, vector.z);
			rb.AddForce(movement * moveSpeed);
		}
	}
		
	void OnTriggerEnter(Collider other){
		if(other.gameObject.CompareTag("Coin")){
			Destroy(other.gameObject);
			AudioSource.PlayClipAtPoint(coinGet, transform.position);

			coinCount += 1;

			if(coinCount == clearCount){
				AudioSource.PlayClipAtPoint(stairsSound, transform.position);
				stairs.SetActive(true);
			}
		} else if(other.gameObject.CompareTag("EnemyShell")){
			Destroy(other.gameObject);
			SceneManager.LoadScene(floorNumber + "F");
		}
	}

	// ★コインパネル
	public int Coin(){
		return coinCount;
	}
}

③CoinPanel.cs

using UnityEngine;
using System.Collections;

public class CoinPanel : MonoBehaviour {

	public GameObject[] icons;

	public void Updatecoin(int coin){

		for(int i = 0; i < icons.Length; i++){
			if(coin <= i){
				icons[i].SetActive(true);
			} else {
				icons[i].SetActive(false);
			}
		}
	}
}

11)触れると隠れていた何かが出現する(敵ボス・隠れコイン)

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-09-09-9-48-32

using UnityEngine;
using System.Collections;

public class BadCoin : MonoBehaviour {

	public GameObject bossEnemy;
	public GameObject coin1;
	public GameObject coin2;

	void OnTriggerEnter(Collider other){
		if(other.gameObject.CompareTag("Player")){
			Destroy(gameObject);
			bossEnemy.SetActive(true);
			coin1.SetActive(true);
			coin2.SetActive(true);
		}
	}
}

12)途中で背景の色を変える。BGMを切り替える。

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-09-09-11-36-51

using UnityEngine;
using System.Collections;

public class BadCoin : MonoBehaviour {

	public GameObject bossEnemy;
	public GameObject coin1;
	public GameObject coin2;

	// ★BGMを切り替える。
	public AudioClip bgm2;


	void Update(){
		transform.Rotate(new Vector3(90,0,0) * Time.deltaTime);
	}


	void OnTriggerEnter(Collider other){
		if(other.gameObject.CompareTag("Player")){
			Destroy(gameObject);
			bossEnemy.SetActive(true);
			coin1.SetActive(true);
			coin2.SetActive(true);

			// ★背景の色を変える。
			GameObject.Find("Main Camera").GetComponent<Camera>().backgroundColor = Color.red;

			// ★BGMを切り替える。
			AudioSource audioSource = GameObject.Find("Main Camera").GetComponent<AudioSource>();
			audioSource.clip = bgm2;
			audioSource.Play();
		}
	}
}