0
\$\begingroup\$

enter image description here
i am making a 2d platformer in unity like mario or adventure island, and i am placing enemies manually(not doing any spawning) in the world, and those enemies have animations and movement scripts with update function.

And now my question is, how can i handle the enemies outside camera view.
I just want that the enemies should update/animate/move only when they come inside the camera view, otherwise they should be fixed where they were manually placed in the world.

Maybe not only enemies but other things like coins, stars, etc(having different animations and scripts)

Should i need to use some collision trigger, like attaching trigger to camera and detecting each enemies entered inside it and enabling animations and scripts on them.
Please tell me if that is an efficient way or if not please tell me if there is any simple and efficient way to do this.
Please correct me if there's anything wrong. Thank you.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Have you considered hooking the OnBecameVisible MonoBehaviour message or using the Animator Culling Mode for this purpose? \$\endgroup\$
    – DMGregory
    Aug 19 '20 at 11:22
  • \$\begingroup\$ Thank you DMGregory, didn't know about that, this is useful and what i needed. \$\endgroup\$ Aug 19 '20 at 13:15
  • \$\begingroup\$ If you find a solution that works for you, please share it as an Answer below that can help other devs in the future. \$\endgroup\$
    – DMGregory
    Aug 19 '20 at 13:16
1
\$\begingroup\$

I handled that by using pooling (i.e. just enable/disable) for the interactive enities that are far/close to the camera view. The tricky part is do you want the logic to resume or restart upon reenabling them. In entity code you should be careful what values you set to be cached (or reset) when you disable entities (you can use OnEnable/OnDisable methods to do that). Another thing to remember is that coroutines are stopped on disabling, while (Fixed)Update behaves more akin to a pause. But you can still get around that by caching or re-initializing values.

I use 2 rectangular trigger-colliders attached on the camera. The smaller rectangle is used as a region to spawn the entities before they appear on the screen. And the far bigger one to despawn them. The despawn region is bigger to avoid common situation where an entity is despawned just after spawning it due to its initial movement being in direction away from the screen. This approach of 2 colliders with different sizes requires your spawner to check if the entity that is to be spawned is not already spawned (and just went outside of the smaller spawn region but is still inside the bigger despawn region). I.e. each spawner should always know if the entity/entities it controls is/are currently enabled or disabled to avoid double spawning.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks Nikaas will try this, very useful. \$\endgroup\$ Aug 19 '20 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .