Arkanoid in Unity - Dealing with collisions between ball and paddle
Hello !
+++
I am starting out in Unity, and for a first project I would like to make a simple Arkanoid clone in 2D.
+++
I have walls with 2D box colliders that define the limits of where the paddle can move (as well as allowing the ball to bounce off it), I have a ball with a specific 2D physics material with {friction = 0; bounciness=1} + 2Dcirclecollider + 2Drigidbody (dynamic), then I have a paddle with another 2Drigidbody (dynamic with fixed Y component and fixed rotation on Z) and a 2D box collider.
+++
I am moving the paddle directly with the mouse, setting up an adequate velocity vector on each PhysicsUpdate(). xmax and xmin variables are set up "by hand" in the editor, and rb is the rigidbody, extracted in the Start() function by a GetComponent.
void FixedUpdate()
{
float p = Input.mousePosition.x;
float new_x = (xmax - xmin) / Screen.width * p + xmin;
rb.velocity = new Vector2(new_x - rb.position.x, 0) / Time.deltaTime;
}
The first problem I had to solve was how to make it so that the paddle does not clip through the walls - i've set the paddle to be a dynamic rigidbody, and also set collision detection to continuous to prevent "teleporting" collision issues.
+++
Another problem I had to deal with, was that the collisions were making the ball lose energy even though bounciness was set to 1 on the ball. I saw a thread with people having similar issues, and adapted a code from that thread to apply a repulsing force everytime the ball colliders, such that the energy stays the same
private void OnCollisionEnter2D(Collision2D col)
{
// Normal
Vector2 N = col.contacts[0].normal;
//Direction
Vector2 V = rb.velocity;
// Reflection
Vector2 R = Vector2.Reflect(V, N);
rb.AddForce(rb.mass * (R - V));
}
Now the ball wasn't losing any energy upon hitting walls, but it was still losing some energy when hitting the paddle. I have learnt that it is because the energy was going from the ball to the paddle, but the paddle has fixed Y position so it would just sink the energy. In order to tackle that, I set up the mass of the paddle to be very big, this seems to solve the issue.
+++
However, I am encountering a fourth problem, and I have no idea how to deal with this one. If I move the paddle too fast, and it hit the ball, the paddle will transfer its energy to the ball - the paddle has an absurdly high mass so the ball gets a super high velocity.
+++
Now that i have exposed my problem, my question is : is there a way for me to desactivate the transfer of energy from the paddle to the ball? Given that i already have a code OnCollisionEnter that applies a force that is supposed to do the job, I don't want Unity to apply another force on top of that.
+++
I saw that there were ways to deactivate collisions between two layers, but this is not what I want - since if i desactivate the collision between the ball and the paddle I will also desactivate the callbacks to OnCollisionEnter and the ball won't bounce at all. Instead, I would like to desactivate the interaction between the ball's rigidbody and the paddle's rigidbody, but just that particular interaction, without affecting the collision code.
+++
Thanks for your answers! And please excuse my weird +++ typing - this is my poor man's attempt at trying to write paragraphs in answers.unity.com