Currently I am making a game project for myself to learn as a hobbyist and I’m creating a sandbox defence game where I can click the monster icon to spawn a monster that goes towards my defend point, in which I have to shoot them until they disappear and die.
Now the problem is whenever I shoot the monster, the bullet knocks it back a little, until the point where it stacks/overlaps with other monsters. This becomes a problem because when they’re closely stacked together and I shoot them, the bullet does not only hit the first target it touches, but hit all of the overlapped monsters instead.
So what is the solution here? I don’t want to remove the Physics2D.IgnoreLayerCollision from my instantiate spawn button as I want my monsters to pass through each other at a given speed.
If there’s any missing information I will try my best to explain it.
You can modify the collision matrix of physics settings to prevent some layers like monsters which you could define from colliding with itself.
Also, if you don’t want to cause physics collision between bullet and monsters, then make bullets triggers instead (assuming your bullets are not raycasts), and check for OnTriggerEnter in monsters to detect a hit.
No I intended to make the bullet to push back the monster a little when it hits on them. And yes I am not using raycast since I don’t want a hitscan type of shooting style.
Maybe I didn’t explain it correctly, but I want the result to not have the bullet push back all of the overlapped monsters, instead I want to push back the closest monster from the overlapped monsters.
@ifancysmeone * “No I intended to make the bullet to push back the monster a little when it hits on them”
Well I can’t read anyone’s mind… anyway do you really need to push the monster? Maybe only create the illusion of it being pushed with animation?
Or keep all the monsters as dynamic bodies so that they will be pushed away from each other when there is overlap. But if the monsters have no free space to be pushed to, there is very little to be done if you push one… then it is a design issue.
I can’t see a situation where your monsters overlap so much that there is no “closest” monster, maybe you should rethink how much the monsters can overlap?