Hi all!
I’m making a Pong clone for learning purposes. I’m not using Unity’s built-in Physics2D system, but I’m manually moving objects by changing their positions by a speed value multiplied by Time.deltaTime.
I’m having problems with checking for collisions between the Ball and the Bats. Currently I’m using Raycasting, with Physics2D.Linecast(prevPos, newPos) method, but this ignores Ball’s Circle Collider and only detects collision at Ball Sprite’s center (because it casts a line between the previous position of the ball and the new position).
How do I detect collision between a Circle Collider and a Box Collider 2D in a continuos way without using built-in physics?
Thank you in advance! ![]()
3 Answers
3For a ball collisions, Physics.SphereCast should work (same as a Line/RayCast, except you’re shooting a “thick” line.)
In general, Unity doesn’t have great “at will” collision-checking. Instant code-checking a for box colliding with stuff is rough (there’s no Physics.BoxCast.) Clearly, it contains all the math. But I’m guessing it’s tied tightly to the physic engine, the Oct-tree, … .
Thank you for your answer,but this doesn't work with sprites (I have tried it). It would be nice to have a Physics2D.CircleCast() though. :)
– K1kk0z90_UnityThe 2D physics system is new, so will probably get more functionality later. But, if you need to, you can always use 3D to make 2D. Just lock one axis in the rigidbodies. Then you have access to all "real" collision checks. I'd guess the specialized 2D stuff just runs faster, if you want a 2D phone game with hundreds of sprites.
– Owen-ReynoldsYou can use the physics colliders as triggers.
You do this by:
Collider.isTrigger = true;
Then you can use these messages to control when 2 colliders collide:
Of course, you would probably want to you the 2D equivalents. - [Collider2D][1] - [OnTriggerEnter2D][2] - [OnTriggerExit2D][3] - [OnTriggerStay2D][4] [1]: https://docs.unity3d.com/Documentation/ScriptReference/Collider2D.html [2]: https://docs.unity3d.com/Documentation/ScriptReference/Collider2D.OnTriggerEnter2D.html [3]: https://docs.unity3d.com/Documentation/ScriptReference/Collider2D.OnTriggerExit2D.html [4]: https://docs.unity3d.com/Documentation/ScriptReference/Collider2D.OnTriggerStay2D.html
– SpinnernicholasSo the trigger functions work also if I'm not using the built-in physics engine? I ask because I'm not using rigidbodys and I'm moving the objects using tranform.Translate(). Thank you in advance!
– K1kk0z90_UnityAfter digging deeper, one of the colliders in a collision needs a rigidbody. But you can set isKinematic = false and it won't be affected by the physics engine.
– SpinnernicholasThank you for your answers, but now I solved by using the Physics2D system. I still have control on ball’s speed and direction, and I move it using rigidbody2D.ApplyForce().
Thank you again for your help anyway.
FWIW, this is the kind of thing that Physx is good at, but you could always use the OnCollisionEnter callback that Unity provides, and the collision data that is returned, to reflect the angle of approach.
– iwaldropThe problem is that I'm using Time.deltaTime, so the movement is not continuos. I need to detect collisions continuosly.
– K1kk0z90_Unity