- Home /
Adding force on collision performance question.
I need to add force to both objects when these hit. Would it be a better practice to trigger a boolean value OnCollisionEnter, which will trigger a function inside the Fixed Update (and here I put add force code)? Rather then applying force inside the OnCollisionEnter?
Same thing I'm doing for inputs which will incur and AddForce. I capture an input in an Update (Example: Input.GetKey("left")) which triggers a boolean and in turn triggers the Addforce function inside FixedUpdate
Answer by AndrewGrayGames · Feb 27, 2013 at 07:18 PM
As far as performance is concerned, I highly doubt it should be an issue for a small number of objects - very large numbers of objects would start to tax either the CPU, or GPU that is handling physics calculations. As far as I've observed, Unity handles physics calculations pretty efficiently.
What does concern me, is if you should need to do it this way at all, as you're using physics.
If you're implementing some type of friction on each object, yes having your collision objects set a boolean which tells the FixedUpdate method to start applying friction every frame could work, until the force on the object falls to a certain threshold, where forces on the object are set to zero and the flag cleared.
If you're operating in a frictionless environment (e.g. space), you can get away with just setting the force on-collision. Presumably, your control system will apply forces and torque to the player's object to allow them control, as your AI will do the same to control your CPU-controlled entities.
Thanks Asvarduil. I need to apply friction on my objects, so I'm sticking to my method.
I didn't understand clearly the last part of your third parag. The part of providing a threshold for forces on an object.