# Calculating vectors for two colliding objects

In a game I’m building I have two gameObjects, balls. They are sprites, with spriterender, rigidbody2d’s and collider2d’s. There are two players who can “throw” the balls by pressing a button. Players try to “kill” the other ball by hitting them hard with their own ball.

Currently, I’m doing something like this:

if(lastVelocity>5) {
coll.gameObject.GetComponent(Player).doDamage(lastVelocity);
}

So if the ball’s velocity.magnitude before the collision was greater than 5, that does damage to the other ball. Works nicely for some cases and I can actually play around and fine tune other aspects of the game.

HOWEVER, big issue is that this doesn’t take into account the vector of the balls. So let’s say that the other ball is flying directly up with magnitude 8 and i hit it from below (about the same direction) with the magnitude of 10. The other ball gets damage of 10, but my ball gets damage of 8, because that was the velocity before impact. The other ball didn’t have velocity towards my ball, but the opposite, so it shouldn’t damage be much or at all.

How I’d like it to be is that the vector is taken into account as a multiplier, or similar. The more towards the other ball my ball is going, the higher the damage is, and vice versa.

So, if i know the position, heading, velocity,etc of these two balls, how can I calculate “how much towards” the other ball is my ball going, so I could adjust the damage properly?

The Collision2D pass in the OnCollisionEnter2D() has a relativeVelocity: