# detect which ball is going to be hit

Suppose the white sphere is gonna be thrown in direction of the others like in the image.

How can I detect which sphere is going to be touched (by the white) first?

Is this 3D or 2D? You might want to look into: Unity - Scripting API: Physics.SphereCast or Unity - Scripting API: Physics2D.CircleCast

1 Like

cool, but how would I â€śthrowâ€ť this spherecast forwardly so it would touch one of the balls? I couldnâ€™t figure out some non-heavy way to do this (actually Iâ€™m using raycast).
It is a 3D project.

It works almost exactly like RayCast, with the exception that you supply a radius as well. It will return the closest object that the ball will hit. So, it shouldnâ€™t be too heavy to use.

SphereCast creates a fixed sphere somewhere in the space and if I want to get the nearest ball, I would have to move this sphere forwards (in the white ball direction) until it touches something.

Detail: I want to know where it is going to hit before the white moves, not during its movement, thatâ€™s why Iâ€™m saying the SphereCast may have to move.

The white ball doesnâ€™t need to be moving for the spherecast to work, but you do need to know which direction it will move. Thatâ€™s the direction you send the spherecast, and see what gets hit.

However, Iâ€™m not convinced that a spherecast is the right choice here, and I believe youâ€™ll need to do something a little more complicated. Consider a â€śrealâ€ť pool game, where you can hit the cue ball off-center to generate spin. In those cases, the ball would move along an arc, not in a straight line, so a spherecast wouldnâ€™t work.

But if youâ€™re not putting spin on the ball, then you can use a spherecast in the direction youâ€™re currently â€śaimingâ€ť the cue stick, and that should tell you the first thing the white ball will hit.

1 Like

Thatâ€™s the point, how should I move the spherecast every frame efficiently?

Well, just to be clear, you donâ€™t â€śmoveâ€ť a spherecast. A spherecast is an instantaneous check you perform in a single frame. You just say how far you want it to try to go. Itâ€™s the equivalent of saying, â€śIf I were to move this sphere forward up to a certain distance, what would it hit?â€ť or, â€śWhat things would I hit if I moved this sphere from A to B?â€ť You donâ€™t actually move the sphere, though.

However, you can perform a new spherecast on each frame, using whatever direction your cue stick is currently aiming. Just put the spherecast call in the Update() method, setting the proper direction each frame. Spherecasting on each frame should be fine from an efficiency standpoint.