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 /
avatar image
0
Question by kayusoft · Sep 26, 2019 at 07:17 PM · rigidbody2dcollider2doncollisionenterphisics

How To Decide To Damage Between Colliding Cars

Hi everyone. I try to make 2d bumper cars game. And i have a challanging question for you. Each car has 2d capsule collider and a rigidbody. I use rigidbody to move cars. And when i hit a car, both cars emit oncollissionenter event. I just want to decrease health value of the car which is hit by other. But how can i decide that which one hit the other one? For example:

 private void OnCollisionEnter2D(Collision2D collision) {
     BumperCar bumpercar = collision.collider.GetComponent<BumperCar>();
     bumpercar.TakeDamage(10); 
 }

If i do something like that, both cars take damage. But i just want only one car which is hit by other to take damage. How can i do that? Do you have any idea?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ejoo_pasco · Sep 26, 2019 at 07:39 PM

You could attach several different colliders to the car.

E.g. one for each corner of the car - invulnerable and a little exposed, a car hit there takes little or no damage - and one for each edge i.e. front, back, left, right which are more vulnerable and a car hit there take more severe damage.

So the driving get's a little bit tactical and you need some skill: how to bump with my corner into the other car - without exposing my weeknesses to.

Maybe you could offer different cars with different strong and weak zones for different driving/bumping tactics.

Wanna see the game then :D

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 kayusoft · Sep 27, 2019 at 08:29 AM 0
Share

Thank you for your answer. If i cannot find another way, i will implement this solution ;)

avatar image
0

Answer by Mouton · Sep 26, 2019 at 10:23 PM

Here is a simple solution:


Check the directions of the colliders, if they cross the other collider, they are bumping it. If they both cross each other (front collision), both should lose health.


Since you are working on objects with similar masses, it should be ok.


If you need a more complex solution which imply mass and velocity of the colliders, look at momentum collision solvers.


Here are useful information on momentums: https://www.physicsclassroom.com/calcpad/momentum

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 kayusoft · Sep 27, 2019 at 08:44 AM 0
Share

Thank you for your answer. I am trying to find a solution by calculating their velocity and hit angle. But i am sure rigidbody, collider or other phisics component have some kind of value to help me achive that :) I hope i won't need to calculate it all from scratch :(

avatar image Mouton kayusoft · Sep 27, 2019 at 07:20 PM 0
Share

Do you use a BoxCollider ? If you do and you don't want to implement the solution with multiple colliders, you can check the angle between the hitpoint normal and the Gameobjet forward is below 1. It means the hitpoint and the direction of the GameObject are the same. It requires less computation than the solution I proposed.

avatar image
0

Answer by NetkicksDev · Sep 27, 2019 at 05:23 PM

Like said above, assuming that you hit enemy cars with the front of your car, you can check for collision between a collider at the front your car with any part of the enemy. Then take health from the enemy.

Comment
Add comment · 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
0

Answer by kayusoft · Oct 01, 2019 at 05:38 PM

Hi guys. I was about to give up but i think i have found my solution:

 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.CompareTag("Player"))
     {
         ContactPoint2D contactPoint = collision.GetContact(0);
         float rigidBodyPointVelocityMagnitude = collision.rigidbody.GetPointVelocity(contactPoint.point).magnitude;
         float otherRigidBodyPointVelocityMagnitude = collision.otherRigidbody.GetPointVelocity(contactPoint.point).magnitude;
 
         if (rigidBodyPointVelocityMagnitude < otherRigidBodyPointVelocityMagnitude)
         {
             TakeDamage(otherRigidBodyPointVelocityMagnitude * damageAmount);
         }
     }
 }

Basicly i have found a way to get velocity at hit(contact) point. I assume that car is hit by other if it has lower velocity. This solution seems like working for me :)

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

187 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

Related Questions

how avoided this among my player and the wall? 0 Answers

Newbie Question: Pivot points and rigid bodies etc 0 Answers

How can i store all colliders of colliders which collide with a prefab? 1 Answer

How to connect onCollisionEnter with another rigidbody via tag,Problem with collision touching a rigidbody 0 Answers

How to prevent a 2D Kinematic asset passing through a 2D Box Collider. 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