1
\$\begingroup\$

I'm looking for ideas on how to simulate accurate (as possible) deflection for a projectile moving with a certain velocity in a tile-based 2D game.

**Figure 1** Deflection

The player is able to shoot a projectile to any direction. Right now, I'm casting a ray from the top, bottom, left and right sides of the BoxCollider2D of the projectile and - for example, if the top ray hits a wall - I use Vector2.Reflect to set the new velocity for the projectile (see the upper part of the image).

The problem I'm trying to solve (see the bottom part of the image) happens when the projectile approaches the corner of a BoxCollider2D. That particular situation causes unwanted behavior and I'm thinking of adding 4 additional raycasts (top-left, top-right etc.) for the projectile to check the diagonal collisions (trying it soon).

I'm fairly new to dealing with 2D collision - so I'm interested in hearing about your solutions or ideas, especially if you have dealt with 2D collision / deflection before. Any alternative or more simple solutions are welcome! Maybe this can be done much easier with CircleColliders?

\$\endgroup\$
1
\$\begingroup\$

Use CircleCollider2D in circular object and BoxCollider2D in rectangular objects.

Use a Physics2D Material in both colliders. Set Friction = 0 and Bounce = 1.

Use force to shoot the projectile. Use Impulseas ForceMode2D.

If the circular object is using gravity then it will lose it's exact path after deflection but depends on amount of force applied.

If the circular object is not using gravity then intended path won't change. But this isn't defined as projectile.

In both case you are getting precise angle after deflection cause of zero friction and full bouncy physics material. But the path depends on gravity.

enter image description here

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.