Previously using the Coroutine method we were able to get enemies spawning, this created a lot of gameobjects within our hierarchy.
to clean this up while are game is running we can make these gameobjects spawn within the spawn manager as a child and within a container.
This is a empty object created within the spawn manager. In the code we will let the spawn manager know it needs this to spawn enemies within it.
Adding to the physics in unity topic we have two methods we can call that effect our game objects. In my previous example i used OnTriggerEnter
Which activated the destroy game object instruction. this works because both objects have colliders components attached.
in the unity documentation is it explained like:
Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody. If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.
the other OnCollisionEnter can be used when collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. For example if we wanted to trigger a finishing line to trigger the end of a race. Gameobjects can pass through each other and an event can take place.
Using in physics in unity is done through applying the component “Rigidbody” to your game component. With this attached it will be able to respond to gravity and be able to collide with other game objects.
To help with the collision of two objects you also add a collider. this can be a box, capsule and other types