Collision detection using raycast problem

So, in my game, there are guns that shoot bullets, which, when they “collide” with the player get removed. The removing of the bullets was once an issue, but it has been fixed now.
The one problem that I now face is the collision detection. Every “update()” cycle the script checks for each bullet that currently exists if it is on top of an player (since my game is 2D I generate the bullets on top of players) by casting a raycast that goes down. If the raycast hit a player, the bullet would be removed.
And this has been working with guns that shoot 1 bullet at a time but recently I tried to add shotguns to the game. Shouldve been pretty simple, right? thats what I thought too. Just add another variable “projectileCount” and in the shoot method add a simple for (i in range(projectileCount)) statement and itll run fine. But thats where I noticed a problem: The fireing worked well, but the collision detection not so well. Although I tell the programm to check every frame for EVERY bullet there is if it collides with a player, it only does the collision checks for only a few of the bullets, which results in some bullets to just fly through the player and with that, deal less damage.
How can I fix that? Do I need (or can I) to use another function to detect collisions?

Hmm… In concept, what you describe should work. So, could we see the relevant code, please? There may be an issue in your implementation that is causing it to not work as expected.

By the way, if I may make a suggestion, have you considered having your rays run horizontally, from the bullet’s previous position to its current one? That might be a little more reliable, as it should prevent bullets from “skipping over” characters due to the speed of the projectile.

So I had this problem like for some time now and browsing through the Internet I couldnt find anything. But just before you answered I searched up the topic once more and already found a solution.
You can use the “intersects” method. I dont know why that method would work better that the raycast one but it does.

1 Like

Hah, fair enough–I’ve had much the same happen myself, as I recall, finding the solution just as–or after–I went to post about it.

Well, I’m glad that you found a solution! :slight_smile: