- Home /
OnCollisionEnter2D Questions
If i have a rectangle 2d object and suddenly it overlaps 2 or more other objects at once, will the rectangle's "OnCollisionEnter2D" get called more than once in a single frame? I am instantiating a rectangular object in a game and it "might" overlap with more than one object when i instantiate it. Also, when does the "OnCollisionEnter2D" get called? Is it before Update function or after? Last, will "OnCollisionEnter2D" get called multiple times before or after the Update function? Thanks!
Answer by Stormizin · Mar 07, 2014 at 08:41 PM
Is called at the same time of Update.
Using a rigidbody will call once time only, till other gameObject collide.
what do you mean by "same time" of update? they are different threads? i used box collider2d by the way.
Answer by GameVortex · Mar 08, 2014 at 12:09 AM
In a single frame the Collision event will be called a maximum of one time for each object that has collided with it that frame. The collision event is called in the physics step which happens after Update, LateUpdate and FixedUpdate.
So if it only gets called once,how do you know which object it collided with? also, does this get called before or after the update function?
As i said it gets called after Update. Not only once, but one time for each object. The collision event has the collider it collided with as a variable in the collision parameter. So you can check the name, the tag, the components or anything else to know which object it is.
I see. So for example, if the rectangle object collided with 4 other objects called A, B, C, and D, the function call sequence would be "Update" -> "rectangle.OnCollisionEnter2D (A)" -> "rectangle.OnCollisionEnter2D (B)" -> "rectangle.OnCollisionEnter2D (C)" -> "rectangle.OnCollisionEnter2D (D)" .... next frame ... "Update" .. and so on.... correct? Or would the sequence might NOT be A, B, C, and D? Sorry for asking so many questions.. thanks.
That is correct, except that you have no guarantee of which sequence A, B, C and D will be in, but they will all happen after Update.
Your answer
Follow this Question
Related Questions
OnCollisionEnter2D Questions 0 Answers
Testing for collisions prior to placing an object 0 Answers
Unity 2D Update Release (Tile Map)? 0 Answers
Handling Movement - Animations 1 Answer