Game object crated with factory disappears after moving some amount of distance (SOLVED)


I’ve set up a character controller that allows to shoot in the direction of mouse cursor relative to player.
However, bullets just disappear after travelling twice the distance from player to mouse position. They are still in the scene but just don’t render anymore.
What could be wrong? Could the problem be in Z-coordinates? (Player has z=1, and mouse cursor has z=0)

Here’s the code I am using for creating bullets:

function combat(self)
	local player_pos = go.get_position()
	self.look_dir = vmath.normalize(mouse_pos - player_pos)
	local shoot_angle = math.atan2(self.look_dir.y, self.look_dir.x)
	local bullet_rot = vmath.quat_rotation_z(shoot_angle)
	local props = { dir = self.look_dir }
	factory.create("#projectile_factory", nil, bullet_rot, props)
	print("mouse is at: " .. mouse_pos, "player is at" .. player_pos)
	self.shooting = false

I can attach full script/project, if there is not enough context.



Is this part of: War battles tutorial ?



War battles code was a starting point, yes.
But I expanded it a bit, so I could shoot in any direction, not only in 8.

1 Like


Ya, happens to my game too sometimes when I fast click on factory created buttons.
I increased all the Max allowed values in game.project file and tried few games now and looks ok.



I’m not sure what the factory creates, but the default Sprite Max Count is quite low, so that might be what you need to increase.



Thank you for your answers.
Sadly, it’s not that simple as the sprite count.
I’ve increased it already and low sprite count produces errors in console.
Described behaviour doesn’t - as I’ve said, objects still exist in the scene, they just become invisible.



I think you were right in your first post, it’s the Z pos. If you are using the “dir” property to move the bullet and there’s a Z difference between “mouse_pos” and “player_pos”, the bullet will slowly change Z pos. Try setting “self.look_dir.z = 0” before you use it.



Yes! It worked!
Thank you very much!