Hey everyone.
I’ve been breaking my head trying to solve this problem for a while now, without any good.
I’m developing a 3D pool game where, before the cue hits the white ball, I’m supposed to show a prediction of where the ball is heading to, which is the first impact and where each object will go after the collision (the white ball and the other ball. If its a wall, I just show the white ball’s direction).
Should be simple, right?
Well. With spherecast, the resulting normal vector sometimes ends up wrong, since Spherecast creates a somehow weird polygon that contains the sphere. If the spherecast is on the same size as the sphere, it contains the sphere completely, but has some extra volume. If the spherecast is 91% of the original sphere size, it’s completely contained in the sphere, but part of the sphere won’t be taken in the collision check, thus misleading information that it won’t hit when it actually does.
As spherecast wasn’t working well for me, I decided to move to raycast. Since all spheres are in the same height, I should be able to consider them circles. Thus, I take three points in the surface of that circle by rotating the radius vector: the point with the same angle as the direction vector (the forward point) and the two extremes (opposite 90º points from the direction vector, right point and left point). From those three points, I raycast towards the movement direction. That way I should be able to know for sure if there is going to be a collision and which side will hit first. I take the side that has the smaller distance towards the collision point and, together with the forward point, calculate a new point between them. Then I compare those three raycasts and reiterate. I set up an angle precision limit of 0.1. Should work, right? Theoretically I would be 100% sure if the object would hit something along the way, which would be the first hit and a close enough collision point (by a 0.1º error margin).
Guess what?
In an ambient surrounded by colliders (walls), even when pointing towards other spheres, the raycasts return nothing. Some of them do. Some of them don’t. Some of them even collided with the white ball itself (even though they were cast from inside the collider, or was supposed to, at least). I used the Gizmos ray draw feature to check on the raycast on the 3D environment and it was CLEARLY going through couple colliders and it was still accusing no collision (distance = 0, collision point : (0,0,0) collider = null).
I’m really not sure how to deal with this. The prediction line is supposed to be updated every frame and should be light enough to work on mobile.
Any thoughts?
Any way to solve the raycast problem? Or to try getting more accuracy from spherecast?
(This problem happens in both Unity 5.6.0f1 and Unity 2017.4, where I tested.)