• 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 /
avatar image
2
Question by serialdrifter · Feb 24, 2012 at 01:01 AM · collisioncardamage

Damaging a car depending on force of impact.

Basically what the title says; I have a car, and i want it to take damage when the colliders of the car hit the ground. Depending on the force of impact, or velocity, i want it to take more or less damage. I tried searching around, but didn't find anything.

Could someone point me in the right direction, any help is greatly appreciated,

Cheers!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by adrenak · Feb 24, 2012 at 03:37 AM

You could use Collision.relativeVelocity : here's the link

Using this you can calculate the relative velocity between the car and the object that the car has collided with. Then you gotta use a mathematical constant to find out how much damage you have to apply to the car. Let me write an example : In this example the car takes no damage if the relative velocity is less than 10 and if more that 10 then it suffers damage. You you want the car to take damage even if the relative velocity is less that 10 then remove that lines in have commented as "remove this" :

 var damageConstant : float;
 var carHealth : float;
 
 function OnCollisionEnter( col : Collision ){
 
     if(col.relativeVelocity.magnitude > 10){ //remove this 
 
         carHealth -= damageConstant * col.relativeVelocity.magnitude ; //line 1
 
     }  //remove this
 
     if ( carHealth < 0 ){
 
         destroyCar();
 
     }
 
 }    
 
 function destroyCar(){
 
     //instantiate some fancy particle system for special effects
     //and maybe instantiate a heavily damaged car on its place
     Destroy(gameObject);  //destroy our car
 }    

You will have to change the value of damageConstant and also maybe carHealth to get better results. By changing them you can change the amount of damage that the car can take before getting destroyed.

I Hope this helped!

Comment
Add comment · Show 7 · 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 serialdrifter · Feb 24, 2012 at 03:48 AM 0
Share

Thanks, this is exactly what i'm looking for, although i did encounter 2 small errors when i tried to build it.

Operator '>' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'int'.

Operator '-' cannot be used with a left hand side of type 'float' and a right hand side of type UnityEngine.Vector3'.

avatar image adrenak · Feb 24, 2012 at 01:12 PM 0
Share

oops!Sorry I forgot that relativeVelocity is a Vector3. I made changes in the line commmented as "line1" and the if statement before it. You must use magnitude there to do those operations . Tell me if it works now?!

avatar image serialdrifter · Feb 24, 2012 at 06:48 PM 0
Share

Yes it works now :) Thank you very much!

avatar image devean · Oct 11, 2014 at 09:48 AM 1
Share

superb suggestion hope it works for me ..

avatar image jakethehuman · Jan 14, 2017 at 06:04 PM 0
Share

It works fine! crashing the car into the wall and other cars seems realistic but there is one problem........ crashing it into light objects like a cone or trash can just totals the car! :(

It looked like relativeVelocity only gets the impact force of the other collider (cone or trash can) and not the car!

any solutions?!?!?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Car Collision Damage 1 Answer

collisions for a fpscharacter in a cable car animated 0 Answers

Player take damage on collision with AI 1 Answer

Character controller mess up damage 3 Answers

limit car collisions 1 Answer


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