オリジナルポストカードは”こちら”で好評発売中!

Unity2D / 只見周遊すごろく制作3(プレイヤを移動させる仕組み)

BoardGameのチュートリアルを参考に、プレイヤーのチャとシロが、只見町の各名所を巡るボードゲームの制作に挑戦した時のまとめ。

今回のゲームで参考にしたチュートリアルはこちら
Unity 2D Tutorial How To Create Simple Board Game With Dice Rolling And Turn Based Movement Features

 チャとシロの只見周遊すごろくの概要

サイコロをクリックするだけで遊べる、簡単な2人対戦のすごろくゲームです。

2Dゲームで遊んでいる時の様子

チャの勝ちです
シロの勝ちです

 プレイヤの移動のためのGameObjectを追加

空のオブジェクトを作り、GameControlと名前を変える。ポジションはリセットしておく。

変数の宣言

GameControlスクリプトを使用するUIテキスト
勝った時に最後に出るテキスト:whoWinsText (PlayerXの勝ちです)
今現在どちらのプレイヤの番かを表示するテキスト:
player1MoveText (チャの番です), player2MoveText(シロの番です)

プレイヤのGameObject:player1(チャのスプライト), player2(シロのスプライト)

 GameControlスクリプトの中身

public class GameControl : MonoBehaviour
{
    private static GameObject whoWinsText, player1MoveText, player2MoveText;

//(UI textオブジェクトは後程作成)

    private static GameObject player1, player2;

//プレイヤのGameObject

    public static int diceSideThrown = 0; //移動する数を保持
    public static int player1StartWaypoint = 0; 
    public static int player2StartWaypoint = 0;

//プレイヤ1と2が、それぞれどの地点から動作を開始するかを決めるStartWaypoint変数

    public static bool gameOver = false; //trueになるとゲームオーバー


~~
~~

 }

Startメソッド


    void Start()
    {
        whoWinsText = GameObject.Find("WhoWinsText");
        player1MoveText = GameObject.Find("Player1MoveText");
        player2MoveText = GameObject.Find("Player2MoveText");

        player1 = GameObject.Find("Player1");
        player2 = GameObject.Find("Player2");

//UIエレメントとプレイヤのGameObjectを見つけて制御する

        player1.GetComponent<FollowThePath>().moveAllowed = false;
        player2.GetComponent<FollowThePath>().moveAllowed = false;

//これによりそれぞれのプレイヤが動く

        whoWinsText.gameObject.SetActive(false);
        player1MoveText.gameObject.SetActive(true);
        player2MoveText.gameObject.SetActive(false);

//それぞれのテキストの初期値
//whoWinsTextは非表示
//プレイヤ1の番なので、player1MoveTextを表示
//player2MoveTextは非表示

    }

自分メモ

【初心者Unity】GameObject.Findを具体例付きで解説
>「Hierarchy」の中からゲームオブジェクトを探してくれる
>位置をスクリプト上で得る

 GameControlスクリプトの中身の続き

GameControlスクリプトのMovePlayerメソッド

メインの動きのUpdateメソッドは後に回して、先にDiceスクリプトから呼び出されるMovePlayerメソッドを見てみる。

サイコロを振ると、MovePlayerメソッドは整数のパラメータを1つ取得する。1の場合はプレイヤ1が動くことが出来て、2の場合はプレイヤ2が動くことが出来る。

    public static void MovePlayer(int playerToMove)
    {
        switch (playerToMove)
        {
            case 1:
                player1.GetComponent<FollowThePath>().moveAllowed = true;
                break;

            case 2:
                player2.GetComponent<FollowThePath>().moveAllowed = true;
                break;

        }
    }

Diceスクリプトからの抜粋

 GameControl.diceSideThrown = randomDiceSide + 1;

    //プレイヤが移動する番では、GameControlスクリプトのMovePlayerメソッドが関わってきて、
        if (whosTurn == 1)
        {
            GameControl.MovePlayer(1); //プレイヤ1の場合、パラメータは1
        } else if (whosTurn == -1)
        {
            GameControl.MovePlayer(2); //プレイヤが2の場合は、パラメータは2
        }

        whosTurn *= -1; 
        coroutineAllowed = true; 

Updateメソッド

    void Update()
    {
        if (player1.GetComponent<FollowThePath>().waypointIndex > player1StartWaypoint + diceSideThrown)

    //プレイヤ1が移動しているとき、もしFollowThePathスクリプトのwaypointIndex(現在の位置)が、
    //player1StartWaypoint(今回の開始ウェイポイント) + diceSideThrown(サイコロで出た目の数)と
    //同じになったら、

        {
            player1.GetComponent<FollowThePath>().moveAllowed = false;

    //プレイヤ1はそのwaypointで止まる

            player1MoveText.gameObject.SetActive(false);
            player2MoveText.gameObject.SetActive(true);

    //テキストの表示は次のプレイヤに移る

            player1StartWaypoint = player1.GetComponent<FollowThePath>().waypointIndex - 1;

    //この時、プレイヤがいる現在のウェイポイントが、次の番が来た時の開始ウェイポイントになる。 
        }


        if (player2.GetComponent<FollowThePath>().waypointIndex > player2StartWaypoint + diceSideThrown)
        {
            player2.GetComponent<FollowThePath>().moveAllowed = false;
            player2MoveText.gameObject.SetActive(false);
            player1MoveText.gameObject.SetActive(true);
            player2StartWaypoint = player2.GetComponent<FollowThePath>().waypointIndex - 1;
        }

    //プレイヤ2も同様に


        if (player1.GetComponent<FollowThePath>().waypointIndex ==
            player1.GetComponent<FollowThePath>().waypoints.Length)
        {
            whoWinsText.gameObject.SetActive(true);
            player1MoveText.gameObject.SetActive(false);
            player2MoveText.gameObject.SetActive(false);
            whoWinsText.GetComponent<Text>().text = "チャの勝ちです";
            gameOver = true;
        }

    //プレイヤ1がゴールに到達したら、プレイヤ1が勝った場合のwhoWinsText(チャの勝ちです)を表示、
    //「〇〇の番です」のテキストを非表示にし、ゲームオーバー

        if (player2.GetComponent<FollowThePath>().waypointIndex ==
            player2.GetComponent<FollowThePath>().waypoints.Length)
        {
            whoWinsText.gameObject.SetActive(true);
            player1MoveText.gameObject.SetActive(false);
            player2MoveText.gameObject.SetActive(false);
            whoWinsText.GetComponent<Text>().text = "シロの勝ちです";
            gameOver = true;
        }

    //プレイヤ2も同様に

    }

FollowThePathスクリプトからの抜粋

public class FollowThePath : MonoBehaviour
{
    public Transform[] waypoints;  //waypoint達の配列

    [SerializeField]
    private float moveSpeed = 1f;  

    [HideInInspector]
    public int waypointIndex = 0;  //キャラクタが今現在 存在するwaypointの番号を保持

    public bool moveAllowed = false;  //moveAllowedの変数

 GameControlゲームオブジェクトにスクリプトをアタッチ

GameControlスクリプトをHierarchy上のGameControlオブジェクトにドラッグ&ドロップ

次回はUIエレメントの作成です。


コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です