Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Aug 26, 2018 at 04:16 PM by DonJorris for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DonJorris · Aug 26, 2018 at 12:30 AM · scripting problemvector3velocitycardamage

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DonJorris · Aug 26, 2018 at 04:15 PM 0
Share

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.

avatar image Deivore DonJorris · Aug 26, 2018 at 05:57 PM 0
Share

Glad to hear it, happy developing!

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DonJorris · Aug 26, 2018 at 12:00 PM 0
Share

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

Answers Answers and Comments

233 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges