Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

フロッガー風ゲームの作り方 ~当たり判定~

Last updated at Posted at 2023-11-30

プレイヤー

FroggerBackGround.GIF
※一番下の黄緑色のオブジェクトがプレイヤーです

当たり判定

当たり判定は、プレイヤー側で行いますので、
下のサンプルコードを付けてください

サンプルコード

PlayerTrigger.cs
using UnityEngine;

public class PlayerTrigger : MonoBehaviour
{
    [SerializeField] GameManager gameManager;

    // Player の初期位置
    Vector2 initialPos = new Vector2(0.0f, -6.0f);
    bool isSafety = false;  // 木の上かどうか

    /// <summary>
    /// Player の残機
    /// </summary>
    public int playerRest { get; private set; } = 3;
    /// <summary>
    /// Player が巣に帰る数
    /// </summary>
    public int nestSheet { get; private set; } = 5;

    // 当たり判定
    private void OnTriggerEnter2D(Collider2D collision)
    {
        RiverOnTree(collision);
        PlayerNest(collision);
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        PlayerDead(collision);
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        RiverOnTreeExit(collision);
    }

    // Player が巣に帰ると初期位置に戻す
    private void PlayerNest(Collider2D collision)
    {
        if (collision.CompareTag("Nest"))
        {
            nestSheet -= 1;
            gameManager.GameClear();
            transform.position = initialPos;
        }
    }

    // Player が川を渡る時、木の上の処理
    private void RiverOnTree(Collider2D collision)
    {
        if (collision.CompareTag("Tree"))
        {
            transform.parent = collision.gameObject.transform;
            isSafety = true;
        }
    }

    // Player が木の上から離れた時の処理
    private void RiverOnTreeExit(Collider2D collision)
    {
        if (collision.CompareTag("Tree"))
        {
            transform.parent = null;
            isSafety = false;
        }
    }

    // Player が死んだときの処理(残機 「-1」)
    // Player が川を渡る時、木の上の場合除外する
    private void PlayerDead(Collider2D collision)
    {
        if (collision.CompareTag("Dead")
        || (!isSafety && collision.CompareTag("River")))
        {
            playerRest -= 1;
            gameManager.GameOver();
            transform.position = initialPos;
        }
    }
}

サンプルコードの説明

※ざっくりと説明するだけなので、わからない場合ご連絡ください

[SerializeField] GameManager gameManager;

ゲームマネージャーの所で Game Manager を作りますので、
その時、紐付けします

bool isSafety = false;  // 木の上かどうか

川エリアで、木の上かどうかの判定です

public int playerRest { get; private set; } = 3;
public int nestSheet { get; private set; } = 5;

プレイヤーの残機とNestに入った数です

    private void OnTriggerEnter2D(Collider2D collision)
    {
        RiverOnTree(collision);
        PlayerNest(collision);
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        PlayerDead(collision);
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        RiverOnTreeExit(collision);
    }

上からプレイヤーに
当たった時の処理(OnTriggerEnter2D)
当たっている時の処理(OnTriggerStay2D)
離れた時の処理(OnTriggerExit2D)
(一般的なイベント関数です)
この3つのどれかに下の各コードを入れていきます。

    private void PlayerNest(Collider2D collision)
    {
        if (collision.CompareTag("Nest"))
        {
            nestSheet -= 1;
            gameManager.GameClear();
            transform.position = initialPos;
        }
    }

Player が巣に帰ると初期位置に戻す処理
OnTriggerEnter2D に入れる

    private void RiverOnTree(Collider2D collision)
    {
        if (collision.CompareTag("Tree"))
        {
            transform.parent = collision.gameObject.transform;
            isSafety = true;
        }
    }

Player が川を渡る時、木の上の処理
OnTriggerEnter2D に入れる

    private void RiverOnTreeExit(Collider2D collision)
    {
        if (collision.CompareTag("Tree"))
        {
            transform.parent = null;
            isSafety = false;
        }
    }

Player が木の上から離れた時の処理
OnTriggerExit2D に入れる

    private void PlayerDead(Collider2D collision)
    {
        if (collision.CompareTag("Dead")
        || (!isSafety && collision.CompareTag("River")))
        {
            playerRest -= 1;
            gameManager.GameOver();
            transform.position = initialPos;
        }
    }

Player が死んだときの処理(残機 「-1」)
Player が川を渡る時、木の上の場合除外する
OnTriggerStay2D に入れる

次の項目

UI関連

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?