# Question Syncing OnCollision with Physics Collisions(2D)

Discussion in 'Physics' started by shmador, Mar 5, 2023.

Joined:
Nov 8, 2022
Posts:
4
I'm making a top down golf game where you drag the mouse to shoot the ball in the opposite direction. The ball uses a physics material with: Friction 0, Bounciness 1.

The ball is a 3D model separated by 3 objects: Collider, Model and Scale manager(So I can scale the model without taking rotations into account). When the ball hits the wall I'm scaling it for a squash effect and updating It's rotation so it can roll in the right way.

In scenarios where the player is hugging the wall and then trying to bounce through the wall, or hits an inward corner(OnCollisionEnter already triggered once) I wouldn't detect the 2nd collision and the ball will bounce without the squash and rotate update.

I've tried playing with different collision functions with no success.

I'm wondering if there's a way to detect when the ball bounces to then apply the desired effect instead of relying on Collisions? Or sync the physics collision detection with my own?

Code (CSharp):
1.     private void RotatePlayerToMovementAngle()
2.     {
3.         Vector2 moveVec = rb.velocity;
4.         if (moveVec.magnitude > 1)
5.         {
6.             Vector2 currPos = transform.position;
7.             Vector2 nextPos = currPos - moveVec;
8.
9.             float moveAngle = Custom.Vector2Angle(currPos, nextPos);
10.
11.             playerSprite.transform.rotation = quaternion.Euler(0, 0, moveAngle);
12.         }
13.     }
14.
15.     private void SquashToWall(Collision2D wall)
16.     {
17.         float velocity = rb.velocity.magnitude;
18.
19.         float squashValue = Custom.Remap(velocity, 0, maxMoveSpeed, minSquashValue, maxSquashValue);
20.
21.         Vector2 wallNormal = wall.GetContact(0).normal;
22.
23.         if (wallNormal.x != 0)
24.         {
25.             scale = new Vector3(1 - squashValue, 1 + squashValue, 1);
26.         }
27.         if (wallNormal.y != 0)
28.         {
29.             scale = new Vector3(1 + squashValue, 1 - squashValue, 1);
30.         }
31.
32.         playerScaleManager.transform.localScale = scale;
33.     }
34.
35.     private void OnCollisionEnter2D(Collision2D collision)
36.     {
37.         if (collision.gameObject.tag == "Solid")
38.         {
39.             RotatePlayerToMovementAngle();
40.
41.             SquashToWall(collision);
42.
43.             stretchTimer = 0;
44.
45.         }
46.     }

Joined:
Nov 8, 2022
Posts:
4
OnCollisionExit2D seems to work, However now I'm struggling with getting the contacts from the collided wall

### Unity Technologies

Joined:
May 24, 2013
Posts:
10,778
Yes, there's a whole bunch of physics queries you can use, typically used with a Kinematic body-type.

https://docs.unity3d.com/ScriptReference/Physics2D.html

Joined:
Nov 8, 2022
Posts:
4
Thanks for the reply, I didn't seem to find which specific function could help me determine when a bounce has occurred(I might not understand what most of them do)

### Unity Technologies

Joined:
May 24, 2013
Posts:
10,778
A bounce is a collision response, you only get that with Dynamic bodies. I think you're getting confused a little here. Kinematic bodies don't have a collision response nor react to any forces; that's the point of them.

Physics queries let you determine where you'll contact. How you move and the "response" is up to you.