user65909
user65909

Reputation: 121

How to move an object without going through colliders

I'm using Unity and i am doing a pong game. I would like to be able to move the paddles with the mouse key. I have tried just moving their position but that of course will simply "teleport" them through the colliders along the edge of the playing field. I tried using addForce() and making the rigidbody fixed in the x position, however, what happens is when the ball hits the paddle, it pushes it and the paddle snaps back. All of the ball's energy is lost (there is gravity in my game). How can i move this box collider but not let it over lap other box colliders while moving? Thanks!!!

Upvotes: -1

Views: 1851

Answers (2)

Vaibhav Devadiga
Vaibhav Devadiga

Reputation: 1

I found a setting named Default Max Depenetration Velocity. It is on the Project Setting > Physics > Default Max Depenetration Velocity. It's value will by default be 10. Change it to 100 or any higher value until the gameobject doesn't pass through the collider.

Upvotes: 0

borancar
borancar

Reputation: 1189

Your paddle should be a kinematic (IsKinematic parameter) rigidbody (attached RigidBody2D) collider while the edges should just be a static collider. But, you should control the limits/edges of paddle movement within your script.

If you do things this way, your ball will naturally bounce of the edges and off your paddle. If, however, you want the ball to pass through the edges but notify you of doing so (e.g. lose condition), you should make the edges a static trigger collider (IsTrigger parameter).

Here's a detailed list with all interactions between different types of colliders: http://docs.unity3d.com/Manual/CollidersOverview.html. The messages generated are passed via 2 different functions: OnTriggerEnter2D and OnCollisionEnter2D.

Upvotes: 0

Related Questions