ゲーム制作総合知識⑤(2025/06/07)

※タスク管理やる!
毎朝4つくらいリスティングしていく。(この機能を今日中に実装する~など)。

Unity - Manual: Unity 6.1 User Manual

※検索方法
プログラミング系の探し物は unity6 docs などで「利用している言語/環境 docs」などでページを探し、大体はサイト内に「サイト内サーチ」があるので、知りたい関数名などで検索する。
Propaties:palameter
なお、こういうものは、覚えない・都度調べる ということを徹底。

※構造体
構造体の中にAddforceがありますよ、という意味になっている。

ジャンプの制限

ジャンプを制限する方法
①スペースを押された回数
②接地判定
③ジャンプ⇒着地までの秒数カウント

③⇒②⇒①の順で実装が難しい。取り合えず①でコードを書いてみる。
Updateは毎フレーム実行されるので

using UnityEngine;


public class PlayerController : MonoBehaviour
{
    [SerializeField] private float jumpPower = 5.0f;
    private Rigidbody2D rb;
    private int jumpCount = 0;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (jumpCount > 0) return;
        if (Input.GetKeyDown(KeyCode.Space))
            rb.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);

            jumpCount++;
    }
}

これでは1回きりしかできない。
まず接地判定を追加する。
そのために、GroundTilemapに「Ground」Tagを追加する。手順は以下。

・Hierarchy内GroundTilemapを選択。Inspector内の「Tag」のタブを選択。

Tags下の「+」をクリック。

「Ground」を入力して「Save」。

Tagタブをクリックして「Ground」を選択。

Groundに接した際にjumpCountを0にするコードを末尾に追加。
全体は下記。

using UnityEngine;


public class PlayerController : MonoBehaviour
{
    [SerializeField] private float jumpPower = 5.0f;
    private Rigidbody2D rb;
    private int jumpCount = 0;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

void Update()
{
    if (jumpCount > 0) return;
    
    if (Input.GetKeyDown(KeyCode.Space))
    {
        rb.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
        jumpCount++;
    }
}
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            jumpCount = 0;
        }
    }

}

GetKeyDown:押した瞬間のみ
GetKey:ボタンの状態

※「何もない=0」ではない。NULL、VOID等々…

Vector:方向=位置情報
Time.deltaTime:Unityの内部時間と現実時間を合わせる。

左右移動を追加

using UnityEngine;


public class PlayerController : MonoBehaviour
{
    [SerializeField] private float jumpPower = 5.0f;
    [SerializeField] private float speed = 10.0f;
    private Rigidbody2D rb;
    private int jumpCount = 0;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

void Update()
{
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            transform.position += new Vector3(10.0f, 0, 0) * Time.deltaTime;
    }
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            transform.position += new Vector3(-speed, 0, 0) * Time.deltaTime;
        }
        if (Input.GetKeyDown(KeyCode.Space) && jumpCount < 1)
            {
                rb.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
                jumpCount++;
            }
}
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            jumpCount = 0;
        }
    }

}

現状では「キャラクターが画面外」にはみ出て見えなくなってしまう。
そこで「カメラ」を操作して、追従するように設定する。

Window ⇒ Package Manager。

PackageManager 内の UnityRegistry 内のウインドウで Cinemachine を検索。

「Install」。

インストール作業が終わったら、ウインドウを閉じて
Hierarchy内で右クリック⇒Chinemachine ⇒TargetedCameras ⇒FollowCameraを選択。

Hierarchy内に「ChinemachineCamera」ができる。MainCamera内には「ChinemachineBrain」が追加される。

プレイヤー内のスプライトを「TrackingTarget」にドロップ。

カメラが追従するようになった。死なばもろとも。

コメント

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