I have an event that spawns a physics projectile, then checks it for collisions and when it hits something calls another event and removes the projectile. However, i need to be able to have more than one of this projectiles at once, but it can only check for collisions on the last spawned object.
Is there a way of doing this other than copying the event for every projectile that can exist at once and calling a different event for each subsequent projectile?
The way that I would do it is to add a trait to the object that you spawn that handles the collision logic. I like try to organize the logic and traits so that each object mostly looks out for itself. So if I have a bullet, I give that bullet a āBulletā trait that handles its expire time, its collisions, and its movement speed. I also have a gun, and I give that gun a āGunā trait, that handles listening for trigger pulls and spawning bullet objects. If the ābulletā object that you spawn has the āBulletā trait applied to it, then every spawned bullet will also have that trait and will therefore behave like a bullet, which disappears and spawns an explosion on contact.
This trait simply removes the bullet when it hits something, and it moves the bullet at a speed of 100 in the bullets look direction ( local positive Y direction ).
For my gun I have the following trait:
This just spawns the bullet and sets its transform to the transform of the gun ( this should probably be the transform of an empty in front of the gun instead ). When the bullet is spawned it will already be pointing the the same direction of the gun, and the āBulletā trait will handle setting its velocity to 100 units/second in the direction it is pointing.