【Unity】プレイヤーが消えたら復活させる方法(リプレイ機能)
ゲームを作る上で、リプレイ機能は必須とも言えますよね。
今回は、プレイヤーが障害物などに当たって消えた場合、初めからやり直す(リプレイ)方法を紹介します。
完成動作
シーンに各GameObjectを配置
1.以下のような感じでGameObjectを配置
赤いボール(プレイヤーとして扱う)の名前は今回は"PlayerSphere"とする。
青いシリンダーはプレイヤーが当たったらプレイヤーを消すGameObject(Cylinder)
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-2.png)
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-1024x423.png)
プレイヤーを消すスクリプトを作成
1.DestroyPlayer.csという名前のスクリプトを作成して、以下のコードを記載する。
using UnityEngine;
public class DestroyPlayer : MonoBehaviour
{
/// <summary>
/// 衝突した時
/// </summary>
/// <param name="collision"></param>
private void OnCollisionEnter(Collision collision)
{
// 衝突した相手にPlayerタグが付いているとき
if (collision.gameObject.tag == "Player")
{
// 当たった相手を1秒後に削除
Destroy(collision.gameObject, 1.0f);
}
}
}
スクリプトをプレイヤーを消させたいGameObjectにアタッチ
1.DestroyPlayer.csをプレイヤーを消させたいGameObject(青いCylinderオブジェクト)にアタッチする。
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-3.png)
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-4.png)
プレイヤーに各種設定
1.プレイヤー(PlayerSphereオブジェクト)にRigidBodyをアタッチ
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-5.png)
2.プレイヤー(PlayerSphereオブジェクト)のタグを"Player"にする
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-14.png)
プレイヤーをプレハブ化
1.プレイヤー(PlayerSphereオブジェクト)をプレハブ化する。
※プレイヤーをプレハブ化することで、プレイヤーが消えても再生させることができる。
1-1.HierarchyタブのPlayerSphereをクリックしたまま、Assetsにカーソルを持っていき、そこで離す。
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-6.png)
1-2.HierarchyタブのPlayerSphereの文字の色が青くなり、AssetsにPlayerSphereのプレハブができていることを確認
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-7-1024x388.png)
プレイヤーを再生(リプレイ)させるスクリプトを作成
1.ReplayPlayer.csという名前のスクリプトを作成して、以下のコードを記載する。
using UnityEngine;
public class ReplayPlayer : MonoBehaviour
{
[SerializeField]
[Tooltip("プレイヤーのプレハブを設定")]
private GameObject playerPrefab;
// Update is called once per frame
void Update()
{
// 設定したplayerPrefabと同じ名前(今回は"PlayerSphere")のGameObjectを探して取得
GameObject playerObj = GameObject.Find(playerPrefab.name);
// playerObjが存在していない場合
if (playerObj == null)
{
// playerPrefabから新しくGameObjectを作成
GameObject newPlayerObj = Instantiate(playerPrefab);
// 新しく作成したGameObjectの名前を再設定(今回は"PlayerSphere"となる)
newPlayerObj.name = playerPrefab.name;
// ※ここで名前を再設定しない場合、自動で決まる名前は、"PlayerSphere(Clone)"となるため
// 13行目で探している"PlayerSphere"が永遠に見つからないことになり、playerが無限に生産される
// どういうことかは、22行目をコメントアウトしてゲームを実行すればわかります。
}
}
}
空のGameObjectを作成
1.Hierarchyタブで右クリックをして、"Create Empty"を選択
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-8.png)
2."GameObject"という名前のGameObjectが作成されるのを確認
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-10.png)
スクリプトを空のGameObjectにアタッチ
1.ReplayPlayer.csを空のGameObjectにアタッチ
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-9.png)
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-11.png)
空のGameObjectのReplay PlayerコンポーネントのPlayer Prefabにプレイヤープレハブを設定
1.空のGameObjectのReplay PlayerコンポーネントのPlayer Prefabにプレイヤープレハブ(Asset内の"PlayerSphere")を設定
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-12.png)
![](https://programming.sincoston.com/wp-content/uploads/2021/07/image-13.png)
完成
これでプレイヤーがプレイヤーを消すGameObjectに当たってプレイヤーが消えても、また再生(リプレイ)するようになりました!
ディスカッション
コメント一覧
まだ、コメントがありません