0
\$\begingroup\$

This is entirely for performance. The 2D physics are less expensive, but I require 3D for some scenes. I never need both at the same time. I know you can have 2D with an orthographic perspective in a 3D engine, but what I want is really the physics engine. Also, is there a way of turning off these engines? I´ve made most collisions from scratch and am only using them for some raycasts at the beginning and for some collider/rigidbody.casts in not every, but a lot of frames (If I understand correctly, they are calculated from the physics engine in each FixedUpdate()).

\$\endgroup\$
  • \$\begingroup\$ Did you try it? Where did you run into trouble that we can help you solve? \$\endgroup\$ – DMGregory Nov 22 at 3:29
  • \$\begingroup\$ I tried implementing 3d rigidbodies and colliders in a 2d project. And it works, so now I don´t know if Unity swaps the engines depending on the type of physics you are using. I thought that it had two different engines for each type, so I don´t know if not adding 3d stuff makes the difference. How can I reduce the unnecessary calculations from that extra dimension in the 2d scenes? \$\endgroup\$ – Fernando Nov 22 at 7:32
1
\$\begingroup\$

Yes, you can use both at the same time. Though, you can only have 2D physics interactions with the 2D physics engine. The models are separate from the logic of the physics engines, so your objects can even have 3D models but 2D physics. Your raycasts can be separate from the physics engine too.

You can just turn the 2D rigidbodies on or off. That will enable/disable physics for certain objects. If you want all 3D or 2D physics to be off you can do that too if you have Unity 2017 or later.

The main problem you're having is probably moving the game-objects that have physics enabled on them. Even just rotating an effector will have performance impact.

In my game, I usually had 10+ effectors and I was mistakenly rotating the whole game-object when I just wanted to rotate the model (sprite, because it was 2D) of the game-object. So, my game would constantly re-calculate the physics for each effector on screen and it'd have a huge performance impact. After 20 objects or so, it would have about 1 frame per second on my phone. Then I separated the physics and the sprites of the game-objects, the game had no problems having 50+ objects at 30FPS on my phone.

\$\endgroup\$
0
\$\begingroup\$

First of all, are you sure that the framerate of your game is affected by unnecessary physics calculations? Have you measured it using the Unity profiler? It's usually not worth the time to chase imagined performance problems when you have no profiler data to confirm that they actually matter and no profiler data to compare to in order to check if your improvements actually improved things.

But let's assume that you did profile and did find out that 3d physics calculations are indeed a relevant performance eater.

Can I use the 2d physics engine in a 3d game?

Certainly. The 2D physics engine works very similar to the 3D physics engine, just that it neither considers nor changes the z-coordinate of game objects. You can have a *Collider2D and a Rigidbody2D on an object which also has a MeshRenderer and is rendered by an angled perspective camera.

Also, is there a way of turning off these engines?

In order for a game object to be handled by the physics engine, it needs a rigidbody (2D or regular). So if you don't want to use the build-in physics, just don't add rigidbodies to your game objects. When there are no objects in your scene with rigidbodies, then the physics engine is technically still on, but won't do anything and its resource consumption will be negligible.

However, keep in mind that the physics engine is also responsibel for calling the OnTrigger* and OnCollision* Events and will only call them when at least one of the objects involved has a rigidbody. If you still want to use these events but don't want these objects to get moved by the physics engine either, give them rigidbodies, but make them kinematic (For 2D rigidbodies, set their body type to "Kinematic"; For 3D rigidbodies, enable the "is Kinematic" checkbox).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.