Projectile collision on tiles

Hello, this is my first post here on the forums. I am making a 2d platformer game to serve as my first portfolio project. The player controls a mage, this mage can shoot fireballs out of his staff.

Problem is, the fireballs come out of the tip of his staff, and when he is close to a wall, they actually go through the wall. When the wall is very thick, the fireball travels all the way until the other end and explodes there.

The fireball collider is entirely inside the tilemap collider (with composite) and the collision doesn’t happen.

How do I detect if the fireball was spawned entirely inside another collider? I tried OnCollisionStay2D, I tried that “collider.contains” too, none of those worked.

The fireball has a circle collider 2d and the tilemap has a tilemapcollider with composite collider.

Thanks in advance!

Lotsa ways to do this.

To get the look you want, one approach is when you cast the spell, do an initial raycast from the center of the player to the tip of the staff, turning off the player’s collider(s) momentarily, then turning them back on.

If you do that raycast and hit something, then that means the staff is in the wall, so the spell fizzles, or maybe just explode the spell effect right against the wall, don’t ever even bother with a projectile.

OR… you can just cheese it and prevent the player from getting so close to the wall

OR… shorten his staff to achieve the same effect as above.

OR… you could just launch the bullet from nearer to the player center, perhaps the grip of the staff, if that can be guaranteed not to go into the wall.

I also recommend using raycast (or two or three of them) each frame to detect impacts rather than a circle collider. The reason is a sufficiently fast fireball could pass straight through a small target. You can set collision checks to continuous but I still prefer the direct control of raycast.

1 Like