- Home /
Bounce not always triggering
I am using a system of trigger coliders and kinematic rigidbodies to detect collisions between objects, using the "OnTriggerEnter" function.
This works just fine to detect all collisions within my game, however, I also want some projectiles to be able to bounce off of surfaces.
The OnTriggerEnter function does not have a normal, and as such, I created one using a raycast:
RaycastHit hit;
if (Physics.Raycast (transform.position, direction, out hit)) {
direction = Vector3.Reflect (direction, hit.normal);
Where the "direction" variable is the direction that the projectile is currently traveling.
Now, the code always executes up to the "if" condition (that is, the game always successfully detects a collision), it does not, however, always return true on the raycast.
I looked into it and it seems as if my problem was that the raycast sometimes started from within the colider (the projectiles I used for testing purposes were fast, this problem was not fixed by using continuous detection). To remedy this, I moved the starting point of the raycast back slightly:
RaycastHit hit;
if (Physics.Raycast (transform.position + (-direction * 2), direction, out hit)) {
direction = Vector3.Reflect (direction, hit.normal);
Which detected many more of the collisions. However, there as still some issues:
It still does not detect all collisions (main issue).
The normal is sometimes not calculated correctly, basically making the projectile bounce back the same way it came from (I am assuming that this is from the change in the raycast start point, but I cannot find a way to manually create raycasts in order to "fix" the normal).
My wall that I use for bouncing is created from separate segments. The projectiles can occasionally slip through between two of these segments. Is there any way to remedy this? (This is more of a "bonus" question than anything, but I will have to fix this at some point)