Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
7
Help us understand the problem. What is going on with this article?
@Eureka

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

More than 3 years have passed since last update.

概要


画面をスクロールする際にプレイヤーの移動範囲に制限を付けたい場合、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(制限したい値, 制限したい値の下限, 制限したい値の上限)としてあげればよい。

7
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Eureka
プログラミング初心者。Unityでのゲーム作りを通してプログラミングを学んでいます。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
7
Help us understand the problem. What is going on with this article?