
前回unityで、簡単なブロック崩しゲーム3Dを作りました。 今回はその続編で、ボールを逃したら、「ゲームオーバー」の画面を表示させる 「シーン移動」それから、リプレイボタンをおして、もう一度ゲームが実行できるように ボタン操作、について解説します。
コード
▼BallScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;//5)シーンを読み込む呪文の巻き物
public class BallScript : MonoBehaviour
{
//1)Rigidbodyが扱える変数を用意
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
//2)Rigidbodyを取得して代入
rb = GetComponent<Rigidbody>();
//3)右奥方向に力を加える(x,z軸)
//[3-1]力を加える方向を決める
Vector3 myDirection = new Vector3(5, 0, 5);
//rb.AddForce(力を加える方向, 力の加えるモード);
rb.AddForce(myDirection, ForceMode.Impulse);// [3-2]インパルスモードで瞬間的に力を加える
}
// Update is called once per frame
void Update()
{
//4)ボールが枠の外に出たら
if (transform.position.z<-8)
{
//Debug.Log("はみ出たよ");
//Time.timeScale = 0;//ゲーム時間を止める呪文
//6)シーンを呼び出す
SceneManager.LoadScene("GameOverScene");
}
}
}
▼ButtonScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;//2)シーンを読み込む呪文の巻き物
public class ButtonScript : MonoBehaviour
{
//1)ボタンが押されたら(外部で操作するのでpublic)
public void ReplayButton()
{
//3)シーンを呼び出す
SceneManager.LoadScene("SampleScene");
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}