The question is answered, right answer was accepted
Damage two objects based on their relativ speed to each other
I am trying to write a damage system, similar to Flatout 2, but I can't find a good solution. I got two cars, each is trying to hit the other with the highest speed possible. If car1 hits car2, which is not moving, car2 should get the damage of the magnitude of the colliosion strength. if car1 hits car2, which is moving towards car1, car1 should also get damage. The problem I am not able to solve is happening when car1 is hitting car2 in the side, while car2 is moving perpendicular to car1. If thats the case only car2 should get damage and car1 should not, because car2 is not moving towards car1.
I tried using the dot product of both relativ speeds of the cars, but couldn't get it to work... If someone knows a method or way to deal damage based on the relativ speed of the object, that the player is colliding with in comparison to the players transform, please tell me. I hope you get what I am trying to explain, any help would be highly appreciated. Thank you!
Answer by Deivore · Aug 26, 2018 at 02:50 PM
One solution is to get the dot product of a car's velocity and the vector towards the center of the other car's front bumper (maybe slightly offset towards the back), and use that ratio somewhere in the damage calculation. Damage occurs if and only if the dot products of the car's velocities is below some threshold.
Case1: You T-bone another car. You have a forward velocity component, and the Vector from front your car's center to the front and center of the other car is positive. Positive dot product, you damage them.
Case2: You are T-boned. You have a forward velocity component and the other car's front is behind your car's front from your perspective. Negative dot product, you don't damage them (and they damage you).
Case3: Driving in the same direction and colliding. The dot product of your velocities is too high and no one takes damage.
Case4: You T-bone, but only barely clip them with your left front bumper. Both cars have positive dot products towards each other and both cars take damage.
Case5: Head-on collision. Both cars have positive dot products towards each other from their frame of reference and both cars take damage.
Thanks to both of you. I got what I wanted using your suggestions. $$anonymous$$y final code looks like this: Vector3 otherVelocity = collision.collider.gameObject.GetComponentInParent().velocity; float damageAngle = Vector3.Angle(collision.relativeVelocity, (otherVelocity)); if (damageAngle < $$anonymous$$DamageAngle) damageAngle = damageAngle 0.2f; damageAngle = damageAngle / 180; damage = damageAngle collision.relativeVelocity.magnitude * OverallDamage$$anonymous$$odifier; collision.collider.GetComponent().GetDamage(damage);
It returns a damage float from zero to one, which can be multiplied by another factor.
Answer by eses · Aug 26, 2018 at 10:17 AM
Hi @DonJorris
I know very little about physics, and this definitely wouldn't be realistic... but an idea.
I'm thinking this from imaginary TakeDamage method's viewpoint:
How about you multiply damage caused by velocity by direction of hit?
If forward or back hit, full damage, if closer to perpendicular hit, 0 damage to self. Vector angle could be enought for this purpose.
You could also then multiply this by the velocity of colliding object, i.e. head on or rear collision to non-moving car will give less or 0 damage to car that is ramming static car.
Thanks for the reply @eses, that is exactly what I am trying to achieve, but I am not able to calculate the correct angle of the hitting cars, as both cars are most likely moving in different directions, when they collide. I am using something like this: FixedUpdate:`relativRBvel = transform.InverseTransformDirection(rb.velocity);`// Get relativ car velocity; OnCollisionEnter() Vector3 otherVelocity = collision.collider.gameObject.GetComponentInParent().velocity; float damageAngle = Vector3.Angle(rb.velocity, collision.collider.transform.InverseTrasformDirection(otherVelocity));
The output is rather random, and similar on each car.
Follow this Question
Related Questions
Vector3 magnitude broken? 1 Answer
Why wont my script work 1 Answer
Unexpected symbol 'return' in class, struct, or interface member declaration 3 Answers
Kicking at 45° direction 1 Answer
My bullets don't fire properly 0 Answers