8
8

More than 5 years have passed since last update.

プレイヤーの移動範囲を制限する

Posted at

概要


画面をスクロールする際にプレイヤーの移動範囲に制限を付けたい場合、Mathf.Clampを使用して常に値が超えないようにするようにするのが便利。

ソース


private Vector2 player_pos;

    void Clamp()
    {
        player_pos = transform.position; //プレイヤーの位置を取得

        player_pos.x = Mathf.Clamp(player_pos.x, -4.8f, 4.8f); //x位置が常に範囲内か監視
        transform.position = new Vector2(player_pos.x, player_pos.y); //範囲内であれば常にその位置がそのまま入る
    }

Mathf.Clamp(float value, float min, float max)で、Mathf.Clamp(制限したい値, 制限したい値の下限, 制限したい値の上限)としてあげればよい。

8
8
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
8
8