Search Unity

Question Unity physics VS self written buil-in pseudo physics questions for character controller.

Discussion in 'Scripting' started by Mori_X, Mar 28, 2023.

  1. Mori_X

    Mori_X

    Joined:
    Mar 17, 2023
    Posts:
    25
    I had to post my questions in this section, because I need answers from the programming side. Hope someone can give me the proper answers. Thanks in advance!

    It’s about the comparison of a completely handwritten built-in character with pseudo physics, or a character controller that uses unity physics without kinematics!

    In comparison:

    Does the character controller with unity physics take more performance, especially when you plan to upscale actions and use it for mobile too?

    With Unity physics these days, is it possible for me to encounter physical glitches, especially when two character controllers clash with their velocity? This brings up the main question, can I fully control a unity physics character controller without getting any glitches at all?

    Or is a built-in pseudo physics written character controller still better, because of the full control, that allows a low overhead regarding performance and avoids uncontrollable glitches, despite it being more work at all, because every case has to be considered.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,739
    Only YOU can answer this by making an instrumented build and running the profiler, NOT in the Unity Editor but rather on the actual hardware that you intend to target.

    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.

    All of your questions seem to just be engineering questions. Yes, you can engineer ANYTHING to do anything, including being smooth and including being glitchy. It's up to your engineering.
     
  3. Mori_X

    Mori_X

    Joined:
    Mar 17, 2023
    Posts:
    25
    upload_2023-3-29_19-39-43.png

    That's the answer I was looking for! Thank you for your time!
     
  4. StarBornMoonBeam

    StarBornMoonBeam

    Joined:
    Mar 26, 2023
    Posts:
    209
    - Only you will know after making the code.

    A self written physics machine will be more performant while you make it, because you will have a physics system that is only partial and so it will perform better. But when it is complete and matches the Unity physics in all functionality will it perform better. Well, if the pursuit is more control then more control can be found by writing something for yourself. At least you could understand or identify what it is about the system you don’t like if that was a system you had total control in the making of.
     
    Mori_X likes this.
  5. Mori_X

    Mori_X

    Joined:
    Mar 17, 2023
    Posts:
    25
    Thank you for this answer! Confirms what I suspected. As with everything in life, only the hard way is the right way.
     
  6. StarBornMoonBeam

    StarBornMoonBeam

    Joined:
    Mar 26, 2023
    Posts:
    209
    no problem I think gravity is very easy to program and enforce over all of your objects that need it but collisions and their forces could get tricky. Many of my games wind up with scripted forces. Very basic stuff. My systems in the past you couldn’t do things like make a set of scales. But you could do that with the Unity one.
    Sometimes we want physics but we don’t want the whole thing, scripted force applications can be nicer and easier to control or limit. Though it can all be done with the rigidbody system, a slimline custom physics character controller, and maybe a physics object reactor, who merely reacts temporarily using some custom physics, sometimes this is all we are after.