Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by MonkeyPetteri · Oct 20, 2014 at 04:55 PM · collisionvector2

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?

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
0

Answer by robertbu · Oct 20, 2014 at 05:14 PM

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

http://docs.unity3d.com/ScriptReference/Collision2D-relativeVelocity.html

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 MonkeyPetteri · Oct 20, 2014 at 05:24 PM 0
Share

Thanks, but that is the relative velocity of the two objects, i.e. it's the same for both colliding objects. I'd need to have one number for ball1 and another for ball2, depending on their vectors..

avatar image robertbu · Oct 20, 2014 at 07:10 PM 0
Share

Why do you need one number for each ball? The magnitude of relativeVelocity gives you a measure of how hard the two balls hit. Say the two balls were chasing each other and one was traveling at a speed of 999.999 and the other was traveling at a speed of 1000, the impact is going to be $$anonymous$$or for both objects. If the mass is different, then you may want to dig deeper.

"The more towards the other ball my ball is going, the higher the damage is, and vice versa."

If this is really how you want to handle it, Vector3.Dot() can calculate a value for you:

http://docs.unity3d.com/ScriptReference/Vector3.Dot.html

http://www.mathsisfun.com/algebra/vectors-dot-product.html

If you normalize the two velocity vectors passed (not necessarily what you want to do), then a value of -1 means they are going in opposite directions. A value of 1 means they are going in the same direction, and a value of 0 means they are moving at right angles with respect to each other.

Again not necessarily what you want, but Vector3.Angle() of the two velocity going into the collision will give you an unsigned angle that gives you a value you can use to see the relationship between the two objdcts.

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

2 People are following this question.

avatar image avatar image

Related Questions

Create a collider for area created by mouse 0 Answers

c# Destroy upon collision isn't working 2 Answers

Tilemap Collision Issues 0 Answers

Ignore collision in particular situation 3 Answers

Non - Physics collision System 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