LoginSignup
2
1

More than 5 years have passed since last update.

Unityで、画面外(カメラ外)に出たかどうか判定する

Last updated at Posted at 2018-01-30

画面外に出る対象物にSpriteRendererコンポーネントをアタッチ

image.png

画面外に出る、たとえばキャラクターや弾等で、下記スクリプトを実装

:Player.cs


void Update(){

    if (!GetComponent<SpriteRenderer>().isVisible)
    {
        Debug.Log("画面外");
    }

}

味噌はこれ

isVisible

注意点

  • ただし、Gameビュー、Sceneビュー両方を開いていると、Sceneビューのサイズをつかっているような挙動をするので注意

 //このオブジェクトがGameViewのみで描画されているか判定用に、
        void OnWillRenderObject()
        {

#if UNITY_EDITOR

            // Debug.LogError("isVisible:::::camera:" + Camera.current.name);

            if (Camera.current.name != "SceneCamera" && Camera.current.name != "Preview Camera")
#endif
        }
2
1
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
2
1