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 /
avatar image
1
Question by Fantom · Sep 04, 2011 at 07:42 PM · collisionvelocity

Velocity before collision

I have some problems, with velocity. I want play sound when rigidbody collide something or other object collide with that rigidbody only when velocity is enough big. When i take velocity in OnColliderEnter function I receive very small value and always a same. Now i update velocity in Update() and when collision is detected use last velocity. Maybe exist another way?

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

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

Answer by Eric5h5 · Sep 04, 2011 at 08:10 PM

That's the correct way, although you should use FixedUpdate instead of Update for this because you're dealing with physics.

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 DMGregory · Aug 17, 2019 at 02:32 PM

I nearly used caching as described here, but I was unsatisfied with that for two reasons:

  • It neglects anything that happened over the course of the last physics step that might have changed the velocity just prior to collision.

  • If you need the incident velocity of both members of the collision, then you need a velocity caching script on both of them - so it doesn't work for general collisions against arbitrary bodies

Fortunately, we can reconstruct the velocities using Collision.impulse:

 static Vector3 ComputeIncidentVelocity(Rigidbody body, Collision collision, out Vector3 otherVelocity) {
     Vector3 impulse = collision.impulse;

     // Both participants of a collision see the same impulse, so we need to flip it for one of them.
     if (Vector3.Dot(collision.GetContact(0).normal, impulse) < 0f)
         impulse *= -1f;

     otherVelocity = Vector3.zero;

     // Static or kinematic colliders won't be affected by impulses.
     var otherBody = collision.rigidbody;
     if(otherBody != null) {
         otherVelocity = otherBody.velocity;
         if(!otherBody.isKinematic)
            otherVelocity += impulse / otherBody.mass;
     }

     return body.velocity - impulse / body.mass;
 }

In 2D, we don't have the total impulse just handed to us, so we have to compute it, but we don't need to flip it afterwards, nor pass in "our" rigidbody2D as a parameter:

 static Vector2 ComputeTotalImpulse(Collision2D collision) {
     Vector2 impulse = Vector2.zero;

     int contactCount = collision.contactCount;
     for(int i = 0; i < contactCount; i++) {
         var contact = collision.GetContact(0);
         impulse += contact.normal * contact.normalImpulse;
         impulse.x += contact.tangentImpulse * contact.normal.y;
         impulse.y -= contact.tangentImpulse * contact.normal.x;
     }

     return impulse;
 }

 static Vector2 ComputeIncidentVelocity(Collision2D collision, out Vector2 otherVelocity) {
     Vector2 impulse = ComputeTotalImpulse(collision);

     otherVelocity = Vector2.zero;

     var otherBody = collision.rigidbody;
     if (otherBody != null) {
         otherVelocity = otherBody.velocity;
         if(otherBody.isKinematic == false)
             otherVelocity += impulse / otherBody.mass;
     }

     var myBody = collision.otherRigidbody;
     return myBody.velocity - impulse / myBody.mass;
 }

In testing this, I find it shows small errors for glancing collisions - so there may be an additional factor I'm missing. Corrections to improve the accuracy for these cases are welcome.

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 JiriAtanasovsky · Feb 16, 2021 at 11:40 AM 0
Share

Thank you for this examle! I will try it out.

I only noticed in 2D version that this var contact = collision.GetContact(0);

should probably be this: var contact = collision.GetContact(i);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Move until collision sticks 0 Answers

How do I move this rigidbody? 1 Answer

Sword slashing logic on Gear VR using controller? 0 Answers

Prevent character controller from changing movement direction when colliding with something. 0 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 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