Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 ProHax · Dec 18, 2017 at 09:59 PM · collisionphysicsrigidbodycolliderdamage

Velocity data from OnCollisionEnter is delayed (incorrect)

I'm trying to do damage calculations between game objects on a collision. The damage will be based on the velocity of each game object during the collision. The problem is that the velocity data seems to be delayed. In other words, the velocity for each object that I read during OnCollisionEnter is the updated velocity shortly after the collision. As a test, I did the following:


BEFORE COLLISION
Object A is stationary -> velocityA = (0, 0, 0)
Object B moves directly towards Object A -> velocityB = (0, 0, 32)


CODE DURING COLLISION

 void OnCollisionEnter(Collision collision){
    // Get initial data
    Vector3 theirVelocity = collision.rigidbody.velocity;
    Vector3 myVelocity = collision.relativeVelocity - theirVelocity;
 
    Debug.Log ("My velocity: " + myVelocity + "\tTheir velocity: " + theirVelocity);
 }


I would expect that the 'Their Velocity' value would be (0, 0, 0) because they were stationary right before the collision, but instead their velocity is (0, 0, -10.6) which is what their velocity is directly after the collision. I also tried this using collision.rigidBody.velocity, but that returns the same value.


One thing I noticed is that if Object A is against a wall or in a corner when I hit it, the 'Their velocity' value does return (0, 0, 0). I assume this is because Object A has nowhere to go and therefore Object A's velocity doesn't actually change from (0, 0, 0).


Is there a way to get the velocity data of each game object directly before the collision takes place?

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

3 Replies

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

Answer by MacDx · Dec 18, 2017 at 10:37 PM

Yes, there is a way. You need to save the velocity during fixed update. If you take a look at Unity's Execution Order in the Physics section, you can see that there is an Internal physics update, this is where I think every rigidbody is moved, and every velocity is updated as a result of collisions, then, after that physics update comes OnTrigger on Collision and Coroutines waiting for fixed update, that is why you are getting the affected velocity and not the one before the collision. Like I said if you want velocity before collision, you will need to have a field on your script where you will save the velocity every Fixed Update. Something like this:

 private Vector3 velocityBeforePhysicsUpdate;

 void FixedUpdate()
 {
     velocityBeforePhysicsUpdate = rigidbody.velocity;
 }

So when you check velocityBeforePhysicsUpdate inside your OnCollision or OnTrigger methods, the field should contain the velocity you're expecting.

Hope this helps!

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 ProHax · Dec 19, 2017 at 01:42 AM 0
Share

Worked like a charm thanks! I'm going to take a look at that Unity docs sections, thanks for the link.

avatar image nt314p · Dec 20, 2017 at 12:26 AM 0
Share

Wow this helped me too!

avatar image
2

Answer by lennardbeers · Feb 26, 2019 at 02:46 AM

@MacDx Hey, I know this is old, but i have a question. The solution above works, but it is very performance unfriendly. I don't want to save the velocity every physics frame, I only want to save it, when the object is actually colliding. Does anybody have an idea how to achieve this? I looked into every forum question about this topic, but nobody seems to have a better solution than just bruteforcing it. This can't be the only way :(

Comment
Add comment · Show 4 · 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 andcl85 · Sep 25, 2020 at 03:56 PM 1
Share

Any updates on this?

avatar image lennardbeers · Sep 26, 2020 at 01:22 PM 0
Share

@andcl85 I fixed this problem and the many others I had with the Unity integrated Physics and Collision by simply not using it at all and doing custom collision detection + physics. If the Unity rigidbody physics don't work for your particular case I would recommend you do the same. It's much less work and you don't spend all your time trying to find workarounds. I only use the FixedUpdate() method to apply my calculated position to the transform and after that check for collision myself with simple circle or rectangle collision mathematics. This works especially good for me, because I only have three GameObjects that can collide at all in my game.

avatar image mp20022013 lennardbeers · Oct 29, 2021 at 10:52 PM 0
Share

are you making a game similar to ping-pong?

avatar image lennardbeers mp20022013 · Nov 02, 2021 at 02:35 PM 1
Share

I actually needed this for a clone of the old school game Blobby Volley.

avatar image
1

Answer by beckermt · Mar 08, 2021 at 10:04 PM

@lennardbeers I found a solution for my particular case. I set my colliders to be triggers and used OnTriggerEnter2D instead of OnCollisionEnter2D. I suppose the trigger triggers before the collision does! :-)

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

171 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

Related Questions

Tossing an object with the correct force at any distance from another so that it collides with the other 1 Answer

Is it necessary to attach a rigidbody/colliders to child objects? 1 Answer

Camera gets flung off of the map when the player collides with certain objects. 0 Answers

Rigidbody doesn't rotate after collision 3 Answers

Stopping my player from moving when hitting a wall. 5 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