- Home /
How do I kill/disable something during a Physics iteration?
Hypothetical case:
A projectile that deals damage and should die the moment it hits something.
Two Physics iterations happen before we get to the Game Logic part of the frame.
The projectile hits something on the first iteration. Even if we call SetActive(false) or Destroy, the OnDisable and OnDestroy events won't happen until the end of the frame.
The projectile hits something during the 2nd iteration, even though it should be dead.
Of course I could flag the projectile as dead, but it's a pain in the ass having other systems/objects/components need to take this flag into account every time they interact with the projectile.
Any clean way to do this?
Answer by TheSorm · Apr 02, 2016 at 10:37 PM
you can just use normal collision detection?
void OnCollisionEnter(Collision collision) {
Destroy(gameObject);
}
This will flag the object for destruction at the end of the Render frame. If there are multiple Physics frames before the next Render frame, the object will be destroyed too late.
See this flowchart: http://docs.unity3d.com/460/Documentation/uploads/$$anonymous$$ain/monobehaviour_flowchart.svg
Hm I think you have to use the fixed update method and set the time in EDIT/PROJECT SETTINGS/TI$$anonymous$$E really low ... Or you can try to use is kinematic on the rigid body when it gets a collision http://docs.unity3d.com/ScriptReference/Rigidbody-is$$anonymous$$inematic.html
Your answer
Follow this Question
Related Questions
how to make destructable items 0 Answers
Collision with a coin 0 Answers
How do I break apart an object? 2 Answers
Collect some coins for Points 1 Answer
Is there a way to destroy an object besides Object.Destroy? 1 Answer