ポケモン風RPGの作り方 #1 プロジェクトの作成と素材のインポート【Unity教材】

この記事で得られること

Unityを使ったポケモン風RPGの作り方がしれる

(基本的には動画の流れを書いてるので、詳しいところは動画を見た方がいいです)

解説動画

Unityプロジェクトの作成

Unityのバージョンは2020.3.15f2

2019.4とかある程度新しかったらOK!(ただしベータ版はNGよ)

新規作成から、2Dプロジェクトを作成!

名前と保存先はご自由に!ただし日本語名はNGよ

素材のインポート

素材のインポートはダウンロードしたフォルダをUnityにドラッグ&ドロップすればOK

基本素材はこちらで全部作ってくれてます!

GitHub - GameDevExperiments/Pokemon-Tutorial-Art-Assets: All the art assets needed to follow along with my unity pokemon game tutorial series in youtube
All the art assets needed to follow along with my unity pokemon game tutorial series in youtube - GitHub - GameDevExperiments/Pokemon-Tutorial-Art-Assets: All t...

ただし、ポケモンの画像とかは著作権的に問題あるので、、、モンスターはこちらのサイトから取得しよう!

GitHub - limbusdev/guardian_monsters_artwork: Artwork from the monster RPG Guardian Monsters.
Artwork from the monster RPG Guardian Monsters. Contribute to limbusdev/guardian_monsters_artwork development by creating an account on GitHub.

マップの作成

タイルマップを使ってマップを作成

Playerの配置と移動

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [SerializeField] float moveSpeed;

    bool isMoving;
    Vector2 input;

    void Update()
    {
        // 動いていない時
        if (!isMoving)
        {
            // キーボード入力を受け付ける
            input.x = Input.GetAxisRaw("Horizontal");
            input.y = Input.GetAxisRaw("Vertical");

            // 入力があったら
            if (input != Vector2.zero)
            {
                // 入力分を追加
                Vector2 targetPos = transform.position;
                targetPos.x += input.x;
                targetPos.y += input.y;
                StartCoroutine(Move(targetPos));
            }
        }
    }

    IEnumerator Move(Vector3 targetPos)
    {
        isMoving = true;

        // targetPosと現在のpisitionの差がある間は、MoveTowardsでtargetPosに近く
        while ((targetPos- transform.position).sqrMagnitude > Mathf.Epsilon)
        {
            transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed*Time.deltaTime);
            yield return null;
        }

        transform.position = targetPos;

        isMoving = false;
    }
}

斜め移動の禁止

    void Update()
    {
        // 動いていない時
        if (!isMoving)
        {
            // キーボード入力を受け付ける
            input.x = Input.GetAxisRaw("Horizontal");
            input.y = Input.GetAxisRaw("Vertical");

            // 斜め移動禁止:横方向の入力があれば, 縦は0にする
            if (input.x != 0)
            {
                input.y = 0;
            }
            // 入力があったら
            if (input != Vector2.zero)
            {
                // 入力分を追加
                Vector2 targetPos = transform.position;
                targetPos.x += input.x;
                targetPos.y += input.y;
                StartCoroutine(Move(targetPos));
            }
        }
    }

おさらいと予告

今回は新規プロジェクトから素材をインポートし、マップやPlayerを配置して動かせるとこまでやったぞ!

次はPlayerのアニメーションをやるぞ!

質問対応について

質問はYouTubeのメンバーシップのDiscordで受け付けています。

コメント

タイトルとURLをコピーしました