Hi,
In Unity, collisions happen between Collider components. When we disable a collider, nothing is able to collide with it anymore. Without a collision, the collision methods do not get called anymore.
With this in mind, check Rick’s code again. We have this line, which toggles the value of collisionDisabled
:
collisionDisabled = !collisionDisabled;
If the value is true
, it becomes not true
, which is false
. If the value is false
, it becomes not false
, which is true
.
Next, let’s figure out where we use collisionDisabled
in the code. It’s here:
void OnCollisionEnter(Collision other)
{
if (isTransitioning || collisionDisabled) { return; }
// code
}
||
means ‘or’. return;
terminates the method immediately. If collisionDisabled
is true
, we terminate the method immediately and do not execute the rest of the code.
As you can probably see, we do not disable the collision because we do not disable the Collider and/or the Rigidbody. OnCollisionEnter still gets called. What we do is to check if collisionDisabled
is true
.
Why did Rick name his variable collisionDisabled
? Probably because it’s the simplest name for his idea. He skips the code we usually execute in our game if our rocket collides.
Is this what you wanted to know?
See also: