- Home /
How do I check for collisions when I move objects based on Time.deltaTime?
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! :)
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.
The problem is that I'm using Time.deltaTime, so the movement is not continuos. I need to detect collisions continuosly.
Answer by K1kk0z90_Unity · Dec 08, 2013 at 10:58 AM
Thank 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.
Answer by Owen-Reynolds · Dec 05, 2013 at 04:03 PM
For 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. :)
The 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.
Answer by Spinnernicholas · Dec 05, 2013 at 04:10 PM
You 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.
So 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!
After digging deeper, one of the colliders in a collision needs a rigidbody. But you can set is$$anonymous$$inematic = false and it won't be affected by the physics engine.
Is kinematic should be set to true to ignore gravity and collisions.
Your answer
Follow this Question
Related Questions
Raycast Not Drawing In Target Direction 0 Answers
Raycast Collider Tag not returning correct result 1 Answer
Raycast goes through 1 Answer
Raycast exit point of collider 0 Answers
raycast not colliding 1 Answer