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!
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.
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?
Answer by spooneystone · Nov 06, 2015 at 10:22 AM
Sorry . I meant put the velocity you add in update
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.
I think it would be better if you posted the scripts involved.
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);
}
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.
Still throwed objects get moved before they change direction :(
Your answer
Follow this Question
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