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 /
  • Help Room /
avatar image
0
Question by JasonSpine · Nov 05, 2015 at 11:44 PM · collisionunity52d-physicsoncollisionenter

OnCollisionEnter2D is called after object was shifted

Hello. I've got a following problem with unity Physcis 2D system...

In my game user holds an object with which he deflects objects throwed at him. Sometimes I wan't to give a specific direction (rigidbody.velocity) to deflected objects. I'm setting velocity in OnCollisionEnter2D method, and it works most of the times (in this method I also change object's layer to prevent further collisions). But sometimes the velocity is set after the throwed object was shifted by the player's object, what isn't a desired behavior.

Is there any way to force Unity to execute OnCollisionEnter2D method before physics engine takes action?

I'm using Unity 5.1.0f3.

Best regards!

Comment
Add comment · Show 2
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 spooneystone · Nov 06, 2015 at 01:04 AM 0
Share

Possibly because the physics are usually called in fixed update and on collision enter gets called on that frame, it my be colliding and not getting the correct frame which would explain the "sometimes" . Put your physics code into update just to give it try for now.

avatar image JasonSpine spooneystone · Nov 06, 2015 at 06:56 AM 0
Share

Hi. Thanks for advice, but my rigidbody is not kinematic (I'm using built-in 2D physics engine to get objects in motion), therefore I can't just simply put my physics code into Update.

Is that what you meant or I don't understand?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by spooneystone · Nov 06, 2015 at 10:22 AM

Sorry . I meant put the velocity you add in update

Comment
Add comment · Show 5 · 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 JasonSpine · Nov 06, 2015 at 04:12 PM 0
Share

So you mean to set a bool variable in OnCollisionEnter2D and check that variable in Update -> if true then set velocity?

I tried this and it doesn't work. I can't stop using OnCollisionEnter2D, because I need to access collision contacts from OnCollisionEnter2D to Instantiate collision effect in the right place. I also need to know if the collision actually occured.

avatar image spooneystone · Nov 06, 2015 at 05:27 PM 0
Share

I think it would be better if you posted the scripts involved.

avatar image JasonSpine spooneystone · Nov 06, 2015 at 05:46 PM 0
Share

Ok.

This script is attached to the throwed object:

     void OnCollisionEnter2D(Collision2D collision) {
         // layer set in physcis 2d settings not to collide with player/user object
         gameObject.layer = 13;
         _rigidbody.velocity = -Velocity.normalized * 9.0f;
         // Velocity is a Vector2 that contains object's initial velocity
         // _rigidbody is object's Rigidbody2D instance
     }

rigidbody is non-kinematic

throwed object is meant to be deflected by kinematic rigidbody controled by game user (it can move and rotate very fast, but what matters is that collision is detected). And it works pretty well, but sometimes there are glitches -> throwed object is moved before changing direction (as if it was pushed outside player's object collider first, or for one frame it used physics engine velocity ins$$anonymous$$d of $$anonymous$$e). I wan't to eli$$anonymous$$ate this bug.

player's object is moved within it's component in fixed update:

     void FixedUpdate()
     {
         _rigidbody.$$anonymous$$ovePosition (Position);
         _rigidbody.$$anonymous$$oveRotation (Rotation);
     }
avatar image spooneystone · Nov 06, 2015 at 11:12 PM 0
Share

Ok what behaviour do you get if you put the fixed update code in the update. I know it's not the correct way but fixed update isn't called every frame so when the collision takes place is could cause the unusual behaviour.

avatar image JasonSpine spooneystone · Nov 07, 2015 at 06:30 AM 0
Share

Still throwed objects get moved before they change direction :(

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

42 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

Related Questions

OnTriggerEnter2D not working 1 Answer

Check for collision ONCE 0 Answers

Collider seems bigger than it is 0 Answers

Character hits invisible ghost collision when jumping against objects while pushing against them 0 Answers

OnCollisionEnter Trigger 1 Answer


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