【Unity】ゲームスタートからの秒数をUIに表示し、記録をつけるスクリプト
public class GameManager : MonoBehaviour { public Text time; public Text highTime; private int seconds; private void Start() { StartCoroutine(CountTime()); } // Update is called once per frame void Update () { time.text = "時間:" + seconds; // ハイスコアを更新 if (PlayerPrefs.GetInt("HighTime") < seconds) { // ハイスコアの保存 PlayerPrefs.SetInt("HighTime", seconds); } highTime.text = "記録:" + PlayerPrefs.GetInt("HighTime"); } IEnumerator CountTime() { while (true) { // 1秒待機 yield return new WaitForSeconds(1.0f); seconds++; } } }
【ゲーム開発記】赤いボールを撃ち落とせ①
unityroomにあげる用のゲームを作り始めた。ちなみに前回unityroomに投稿したゲームは、閲覧数が100を超えてた。
作ったゲームが実際に遊んでもらっていることが分かったので、非常にうれしい。
赤いボールからひたすら逃げろ | 無料ゲーム投稿サイト unityroom - Unityのゲームをアップロードして公開しよう
で、今作っているゲームは、シューティングゲームです。
今日の進捗としては、Playerをキーボードで操作し、マウスをクリックすると弾を発射するところまで作った。
毎日少しずつ進めていこう。
【Unity】マウスをクリックしたら、弾を発射する方法
public GameObject bullet; // Update is called once per frame void Update () { if(Input.GetMouseButtonDown(0)) { shot(); } } void shot() { Instantiate(bullet, transform.position, transform.rotation); }
ちなみに、弾の方のスクリプトは以下。
public int speed = 10; Rigidbody rb; // Use this for initialization void Start () { rb = GetComponent<Rigidbody>(); rb.velocity = transform.up.normalized * speed; }
【Unity】GameObjectをキーボードで操作する方法②
public float speed = 10; Rigidbody rb; void Start () { rb = GetComponent<Rigidbody>(); } // Update is called once per frame void Update () { float x = Input.GetAxisRaw("Horizontal"); float y = Input.GetAxisRaw("Vertical"); Vector2 direction = new Vector2(x, y).normalized; rb.velocity = direction * speed; }
【Unity】シーンの再読み込み方法
>|cs|
// 現在のシーン番号を取得
int sceneIndex = SceneManager.GetActiveScene().buildIndex;
// 現在のシーンを再読込する
SceneManager.LoadScene(sceneIndex);
||<
【Unity】ハイスコアの保存と表示
// ハイスコアを更新 if(PlayerPrefs.GetInt("HighScore") < allSeconds) { // ハイスコアの保存 PlayerPrefs.SetInt("HighScore", allSeconds); }
// ハイスコアの呼び出し highScore.text = "HIGH SCORE:" + PlayerPrefs.GetInt("HighScore"); // 変更された値をディスクに保存 PlayerPrefs.Save();
【Unity】Prefabから自動生成する際、生成位置をランダムにする方法
Vector3 vector3; public void createEnemy() { vector3 = new Vector3(Random.Range(-5.0f, 5.0f), transform.position.y, transform.position.z); // Enemyを作成する GameObject enemy = (GameObject)Instantiate(enemyOrigin, vector3, Quaternion.identity); }