Need help figuring out why my gun stops shooting

Ok, I’m trying to use triggers to know when an object is in range. So my tower has a collisionobject (trigger) and the enemy has a collision object (trigger). And it mostly works. But sometimes it just stops firing at the enemy (especially when there are many enemies).

Even if I don’t test for message.enter it will stop. But what is the best way to detect all objects inside of my collisionobject? Clearly I must be doing it wrong.

function on_message(self, message_id, message, sender)
	if message_id == TRIGGER_RESPONSE then
		print(message_id)
		if  message.enter and message.group == ENEMY then
			self.inTargetZone = true
			--fire missile55
			self.target_id = message.other_id
			fire_bullet(self)
		elseif message.enter == false and message.group == ENEMY then
			--reset turret to 0
 			self.inTargetZone = false
			go.set_rotation(vmath.quat_rotation_z(0))
		end
	elseif message_id == hash("entity destroyed") and message.group == entmgr.enemies then
		if self.target_id and message.entity == self.target_id then
			self.target_id = nil -- Clear the target
			-- Additional logic to handle lost target (e.g., redirect missile)
		end
	end	
end

Are you perhaps hitting the sprite limit set in game.project? Do you get an error?

No errors

Ok, and can you confirm that your bullets are spawning? What if you hardcode a position for your bullets?

That helped me figure out what I think is going on. So what appears to be happening is as objects enter the target zone the new aim point becomes the new object in the zone. So it stops firing on the old object and then targets the new one, but then it can never acquire the old one again.

I guess I need to make some code that keeps it tracking the existing one until it leaves the target zone.

But I still wonder how do I pick up and target items that are IN the zone. So let’s say I destroy one object and there are two more IN the zone. It appears it won’t target them…only new ones as they enter the zone. Does this make sense?

I’ve tried triggers and kinematic collisionobjects. Both do the same thing.

You store all viable targets in a table. When a target stops being viable (leaves, is destroyed) remove it from the table. When the current target is no longer viable (same reasons) pick another one from the table.

3 Likes

Thanks…that makes sense.