LoginSignup
0
0

More than 5 years have passed since last update.

Particle SystemにつくColliderのCenter、SizeはそのParticleSystemによって変わる

Last updated at Posted at 2016-03-28

AddComponentをした時のColliderの初期の値が思ってもいないものになってバグったので挙動を調べた。
先にまとめを書くと、以下になる
1.オブジェクトにParticleSystemが存在してそいつが悪さをしていた
2.Rendererが切られていてParticleSystemがついているという発見が遅れた
3.コードからColliderをAddComponentするときは明示的に初期化しよう

まず何もないGameObjectを用意する。
EmptyGameObject.png

BoxColliderをつけると次のようなCenter(0,0,0), Size(1,1,1)のColliderがつけられる。
Collider.png

次に、ParticleSystemにColliderをつける。
ParticleSystemCollider.png

ColliderのSizeとCenterの位置がParticleの飛ぶ範囲に影響される。
これは、ParticleSystemのRendererを切っているものにColliderをつけても同じ結果になる。

Rendererを切ったParticleSystemを用意してColliderをつけてみる。
DisableRenderer.png

DisableRendererCollider.png

RendererがついていないParticleSystemにコード側からColliderをつけようとしてとあるバグを踏んだりもした。

次に、コード側からのColliderのAddComponentの挙動を見る。

StartAddComponent.cs
public class StartAddComponent : MonoBehaviour {

    void Start () {
        gameObject.AddComponent<BoxCollider>();
    }
}

AddComponentStart.png

Startは同じ挙動になるがAwakeは違う。

AwakeAddComponent.cs

public class AwakeAddComponent : MonoBehaviour {

    void Awake () {
        gameObject.AddComponent<BoxCollider>();
    }
}

CentreやSizeが初期値になる。

なので、PrefabをInstantiateしたタイミングでColliderをAddComponentした場合と、1frame挟んだあとにAddComponentしたのでつくColliderのサイズは変わるので注意。

なので、ここらの挙動に惑わされないように、CodeからAddComponentする場合は明示的にCenterとSizeに値は代入しておきたい。

        var collider = gameObject.AddComponent<BoxCollider>();
        // 期待する値を代入
        collider.center = Vector3.zero;
        collider.size = Vector3.one;
0
0
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
0
0