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 manny003 · May 21, 2015 at 01:01 PM · javascriptscripting problemphysics

Need to get physics info on gameObject just before OnCollisionEnter2D

I have a need to know the direction, velocity and other aspects of a rigidbody just before it's acted upon by the physics engine.

It would appear that from inside OnCollisionEnter2D (or 3D as well), the physics engine has already acted upon it and all the info I get is the result AFTER the collision and not just before.

I was hoping for something like BeforeOnCollisionEnter2D but nothing like that exists.

Any suggestions?

Thanks, Manny

Comment
Add comment · Show 1
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 SirBoboHobo · May 21, 2015 at 01:03 PM 0
Share

Have you tried using a ray under your object? when the ray is hitting an object under it, return whatever you need about the object. Raycasting

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Baste · May 21, 2015 at 01:37 PM

If you only need this behaviour from a few objects, you could put a script on them that stores the data from the rigidbody every FixedUpdate, and then poll that data after the collision.

So a script like:

 PhysicsCachedData : MonoBehaviour {
     
     Vector3 lastVelocity;
     Vector3 currentVelocity;

     void FixedUpdate() {
         lastVelocity = currentVelocity;
         currentVelocity = rigidbody.velocity;
         //store current and last of all the other properties you're interested in
     }
 }

and then you could poll it like this:

 void OnCollisionEnter(Collider c) {
     var cachedData = c.GetComponent<PhysicsCachedData>();
     if(cachedData != null) {
         //use cachedData.lastVelocity here
     }
 }

That's really the only thing I can think of. It might be that it's enough to store only data from this frame, if FixedUpdate happens before collisions are resolved - you'll have to check that yourself.

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 manny003 · May 21, 2015 at 01:51 PM 0
Share

That's what I'm doing right now, except I'm using Update ins$$anonymous$$d of FixedUpdate.

I'm just trying to optimize by not doing so many memory moves needlessly all the time as collisions only happen occasionally.

Thanks for your input.

avatar image HarshadK · May 21, 2015 at 02:23 PM 0
Share

In continuation to the method above by @Baste one optimization you can do is extrapolate the position of rigidbody to get the expected time of when the next collision might happen and check for physics values for the rigidbody inside FixedUpdate based off that time value.

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

21 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

Related Questions

driving game power ups 1 Answer

trouble making a fast projectile 2 Answers

Why are these object passing through each other? 1 Answer

I am having a problem trying to simulate football(soccer) physics 2 Answers

How to make object bounce from one bound to another? 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