• 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

2 Replies

· Add your reply
  • Sort: 
avatar image
5
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 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 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.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

168 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

Related Questions

Layers are colliding even though they are set to ignore each other 1 Answer

How to properly move a rigidbody/collider? 2 Answers

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

Preventing objects from intersecting with minimal physics 3 Answers

Kinetic Energy 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges