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

・iPhone専用アプリ(app storeから)
・ソフトウェア;Unity5.3.6
・プログラミング言語;C#
Unityで作成したシンプルなボール転がしゲームです。このゲームのサンプルコードはこのサイトにて随時公開していきます。Unity及びプログラミング学習用に活用してみてください。
1)ボールを動かす/ボールでコインをとる

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)ボールをワープさせる(ワープポイント)

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)ボールを加速させる(加速装置)

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)ボールの大きさを変える

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)落ちる床

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)追いかけてくる敵

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)弾を発射する敵

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)

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)

①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)触れると隠れていた何かが出現する(敵ボス・隠れコイン)

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を切り替える。

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