LoginSignup

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

障害物(車)→道路エリアにある白い動くオブジェクトです。
障害物(木)→川エリアにある茶色い動くオブジェクトです。

障害物(車)

1.Hierarchy → 2D Object → Sprites → Square で、オブジェクトを作成
2.Box Collider2D とサンプルコード(このページの下の方にあります)を付ける
3.タグを「Dead」に変更、Sprite Renderer Order in Layer を -2 に変更
4.Transform Scale x:0.95 Y:0.95 Z:0.95 にする
5.Prefabにする

障害物(木)

車と同様に
1.Hierarchy → 2D Object → Sprites → Square で、オブジェクトを作成
2.Box Collider2D とサンプルコード(このページの下の方にあります)を付ける
3.タグを「Tree」に変更
4.Sprite Renderer Color rgba(180,60,60,255) Order in Layer -4 に変更
5.Transform Scale x:0.95 Y:0.95 Z:0.95 にする
6.Prefabにする
※車と作り方が違うのは、タグと Color と Order in Layer くらいです

サンプルコード

ObstacleScripts.cs
using UnityEngine;

public class ObstacleScripts : MonoBehaviour
{
    // 移動向き
    private enum MoveDirection
    {
        rightToleft,                    // 右から左
        leftToright                     // 左から右
    }

    [SerializeField]
    MoveDirection direction;            // 方向
    [SerializeField, Range(0.0f, 5.0f)]
    float speed;                        // 障害物の速さ

    Vector3 directionX;                 // 移動の向き
    float yPos;                         // 障害物の位置(Y軸)
    float firstPos;                     // 障害物の初期位置
    float endPos;                       // 障害物の最後の位置
    const float MAX_POS = 7.0f;         // 右端
    const float MIN_POS = -7.0f;        // 左端

    private void Start()
    {
        FirstProcess();
    }

    // 初期設定
    private void FirstProcess()
    {
        yPos = transform.position.y;    // 障害物のY軸の位置

        // 右から左の場合の初期処理
        if (direction == MoveDirection.rightToleft)
        {
            directionX = Vector3.left;
            firstPos = MAX_POS;
            endPos = MIN_POS;
        }
        // 左から右の場合の初期処理
        if (direction == MoveDirection.leftToright)
        {
            directionX = Vector3.right;
            firstPos = MIN_POS;
            endPos = MAX_POS;
        }
    }

    private void Update()
    {
        CarMove();
    }

    // 障害物の動きの処理
    private void CarMove()
    {
        transform.position += directionX * speed * Time.deltaTime;        // 障害物の速さ

        // 移動向きが右から左の場合に最後の位置まで到達時の処理
        if (direction == MoveDirection.rightToleft
         && transform.position.x <= endPos)
        {
            transform.position = new Vector3(firstPos, yPos, 0.0f);
        }
        // 移動向きが左から右の場合に最後の位置まで到達時の処理
        if (direction == MoveDirection.leftToright
         && transform.position.x >= endPos)
        {
            transform.position = new Vector3(firstPos, yPos, 0.0f);
        }
    }
}

簡単に説明すると移動範囲(左端 -7.0f ~ 右端 7.0f)
動く方向(右から左及び左から右)と速さ(0.0f~5.0f)です
使い方は、道路エリア及び川の渡り方で説明します

次の項目

道路エリア

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