Managing GameObjects

Laser shots instantiating and being destroyed after reaching a certain threshold (off-screen)

When dealing with game objects, especially those that are spawned during the runtime of your gameplay, you’ll want to take care to ensure their proper instantiation as well as cleanup, so that your object graph doesn’t become overly cluttered and eat up heaps of memory. In the case of Sovereignty, the player’s ship will be able to utilize various projectile weapons, starting with the basic single-shot rapid-fire laser. The spawn of the laser prefab is triggered by user input, specifically the spacebar:

In the Player script, beyond tracking 2D movement input, we spawn a laser prefab with an offset from the ship.

When lasers are spawned, they are given a translation “forward” indefinitely, and once they’ve reached a certain threshold, are automatically cleaned up, as demonstrated here:

After reaching 8f further than anywhere the player can be on-screen, we know the laser is out of sight.

--

--

--

Father, software developer, writer and avid gamer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Visual Design of Games

I’m Bad at Video Games: Why You Should Care

Important stuff you did not know about the PS5 just 48 hours ago

Here’s Why Marvel’s Madripoor Was And Is Wolverine’s Wakanda

Wolverine trailer Tamil Madripoor reference MCU

PlayStation5: the gamer’s review

🔑 GameFi is called the next big thing in the technology world.

3ds Card Reader For Mac

Mixer is gone, is Facebook Gaming the next step?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ahad L. Amdani

Ahad L. Amdani

Father, software developer, writer and avid gamer.

More from Medium

My journey becoming a Unity game developer: Make a game look beautiful-Door, Frame, and Wall…

5 Common Unity Game Development Mistakes

My first game in Unity

Importing Character Models and Animations from Mixamo into Unity