How to make animated factory objects detect collisions

It feels like I’m having problems with every step of this project. Is there any way to get it so that projectiles I am spawning from a factory from a player sprite and animating towards the position of a mouse click, are able to detect collisions? I want to make it so that the projectiles will delete when they collide with the wall of the map and, later down the line, be able to collide with enemies.

Hi @TScriv !

I can recommend the War Battles tutorial that demonstrates this functionality.
It’s available from your editor when you start it.

2 Likes

Hello. So I have made it so that the projectiles fire in the direction the player is moving like in the tutorial. But my aim is to get it to fire towards the click of the mouse and I can’t get that for some reason.


This is the code from the player script
image
And here is what I have on the script attatched to each projectile.

Are you using a camera? If you are using a camera you need to start by converting from the screen coordinate where the user clicked to a position in your game world. There is an example of this in this screen to world sample project: GitHub - defold/sample-screen-to-world-coordinates: This sample project shows how to convert from screen to world coordinates while using a camera component.

I already have this in place and my firing system worked perfectly when I had it so the projectiles were animated.

Ok, so you would like to replace message.shootPos with the world position of a mouse click?

I think that’s already what I have


This is from the cursor script which maps the click and travels to the position of the click

Ok, that should work then. It’s hard to say what’s wrong. In which direction is the bullet traveling if not towards the cursor?

It doesn’t seem to be spawning at all. Or at least not on the player any more.

I’ve since tried to use the animate function and tried to add collision from the script but this doesn’t work


This does however work in terms of firing towards the player click but I can’t get the collision to work

Hint: Please don’t post screenshots of text, as it makes it much harder to read, and above all it’s not searchable. (this is a forum after all, and we want questions and answers to be searchable)

Hello, of course, my mistake.

if message_id == hash("shoot") then
	local pos = go.get_position() -- get player position 
	pprint(pos)
	local direction = vmath.normalize(message.shootPos - pos) -- calculates vector direction of shot 
	local distance = 1000 -- temporary distance for bullets to travel 
	local to = pos + direction * distance -- calculates the intended destination of the bullet 
	--local angle = math.atan2(self.direction.y, self.direction.x)
	--local rot = vmath.quat_rotation_z(angle)
	--local props = { dest = to }

	local bullet_id = factory.create("#pProjectiles_factory", pos) -- creates a projectile from the player projectile factory
	--pprint(bullet_id) 
	-- animates bullet towards location clicked and despawns after a certain distance travelled 
	go.set(bullet_id, "position", pos)
	go.animate(bullet_id, "position", go.PLAYBACK_ONCE_FORWARD, to, go.EASING_LINEAR, distance / 350, 0, function()
		go.delete(bullet_id)
	end)
	--local collision_object = physics.collisionobject("#projectile_collisionobject", {group = "projectiles"})
	--go.set(bullet_id, "collisionobject", collision_object)
end
1 Like

Is someone able to help me with this?

I’ve updated the sample project for screen to world conversion to include spawning of bullets that rotate towards and move to the world position you clicked on. I suggest that you study it and compare with your own project.

3 Likes

:thinking: If you already have the look at (mouse) function, what you should implement is the move to target function.

Understand these instructions: :nerd_face:

And you’ll get what you’re looking for.

It’s not clear to me this:

“I want to make it so that the projectiles will delete when they collide with the wall of the map and, later down the line, be able to collide with enemies.”

A bullet pierces the wall, a missile destroys the wall… :sweat_smile:

2 Likes

https://drive.google.com/file/d/11pAqS6N6743rB7f57x6bBMcP7uEUj8cv/view?usp=sharing

Here’s a short video of what I currently have. The projectiles travel in the direction of my click fine and collide with enemies fine however the enemies do not collide with the walls and neither do the projectiles

Two things:

  1. Enable physics debugging and ensure that the walls are where you expect them to be. Debugging in Defold
  2. Check your groups and masks. Make sure that the group assigned to the walls mask against enemies and projectiles, and make sure that enemies and projectiles mask against walls. Collision groups in Defold

Thank you. I had the wall collision object masked to just the player :man_facepalming: