i am sitting here on a problem since a very long time and I can’t find a solution for that.
Situation: I want to reflect a projectile on a wall, like the image below shows. The reflection works fine so far, but in some cases (1 out of 10) the Projectile goes inside or through the gap between two box colliders. Those colliders are aligned perfectly, with a 1,1,1 colider. Still it seems like the Raycast and the Trigger Event of the collision hits the wall marked with a two before the first side of the collider. Setting the Collision Detection to Continous didn’t help solving this problem. Merging the single colliders to one would help on some parts of the game but it’s not possible to do this on every wall. The Map Building is based of blocks like in a favorite block game. It is a 3D game. Playing around with kinematic and non kinematic didn’t change anything. The projectile got a Rigidbody, while the walls are static collider.
Even Raycasts seem to miss the ,outside bound, of the box colider.
private void GetFutureNormal()
{
Vector3 fwd = rigidbody.transform.forward.normalized;
Ray ray = new Ray(transform.position, fwd);
Physics.Raycast(ray, out hit);
normalToHit = hit.normal;
}
private void OnCollisionEnter(Collision hit)
{
if (hit.gameObject.tag == "wall")
{
Vector3 newDirection = Vector3.Reflect(transform.forward, normalToHit);
transform.rotation = Quaternion.LookRotation(newDirection);
}
}
Maybe you guys can give me some hints to solve my problem, because i simple can’t get around this one. I would be very thankful if we can find a solution together.
The bad news is that I don’t think Unity’s built-in collision is going to cut it. It’s a great and highly performant system, but this specifically is the Achilles heel of general-purpose physics engines - and if your world is constructed entirely out of Greek heels, it’s probably not going to end well.
The good news is that there are two things you can try:
First, try increasing the size of your static colliders so that they overlap rather than aligning. You may find this sufficient for your needs, or you may not - it depends a lot on how fast things are moving, how big they are etc.
Alternatively, with a regular array of cubes it is fairly straightforward to calculate your own raycasted collisions.
The basic algorithm is as follows:
Take current position, and from it figure out which imaginary XYZ box you’re in.
Take velocity, and from that figure out which threshold out of the box you are going to cross first (X, Y or Z)
If the XYZ box you are going into contains a solid cube, reflect velocity in the plane you just tried to cross.
Edge case: If you crossed two thresholds simultaneously (your diagram), check the boxes adjacent to the solid one to see which reflection makes the most sense (possibly both if you’ve fired into a corner)
Go back to 2 and repeat until the movement from this frame is exhausted.
Thank you very much Peeling your answer got me in a good direction. I’ve followed your approach, and now i check the cube next to the one which got hit and get the desired direction based on the free space and the normals which got hit. Now what i didn’t mention above is, that my cubes aren’t alle the same size (some take 0.5/0.5/0.5) others (4/2/3)and they might also have some round elements or don’t fill up a 1/1/1 complete. Now i’ve got two different things i am thinking about.
How do i handle round/different sized elements. The different size shouldn’t be a problem just increase/decrease the reference box size, but the round or other shaped elements give me some headache
If the projectile hits two different cubes at the same frame like in the image above. I have no idea which one is the cube i have to check the cubes next to it. Is it the left one or the right one.
I would be thankful if somebody can lead me in a good direction again.