Replace some animation by scripting

Hello everybody :)!

I’m doing a 3D game on mobile ,
and I got into the bad habit of using animations for lots of small details.
example: a tree that shakes when I cut it, objects that fall to the ground and turn on themselves while floating, and many other little details where I don’t bother to do it in programming.

so here is my question:
is it really worth using as little animation as possible for low performance mobile?
compare to adding 10 scripts each with their own different Update? by grouping those using the same systems as much as possible (example, a single Update to manage objects on the ground that rotate, etc.)

thanks for helping me! have a good day :smile:

Yes I think it is worth.

Animator is good for complex animation a little wasteful for small environmental things. But it is a matter of opinion whether or not the small obj would prefer a script over an animator, but I can tell you as much as animators need objects to attach to. And the object construct is rather expensive in comparison to it’s alternative. So controlling huge group of grass billboards by script to give a nice wind effect or standardized tree wobble will perform better and be reducible in a script.

The Animator component does bear a lot of overhead, compared to the Animation component that is more lightweight.

And despite being ‘legacy’ and Unity recommending the new Animator component, a lot of devs still use the Animation component for more lightweight, basic animation work.

Of course doing simple movement, such as rotating an object on the spot, can easily be done via code.

In the end, don’t guess, profile. Figure out where the performance issues are, and solve those specific problems.

How is that a BAD habit!? Using animation is a GOOD habit!

The BAD habit is writing code for everything… code has bugs.

Even if the code doesn’t have bugs today they will soon grow and appear like worms in a moldy pile of stinky garbage.

If you can animate something, then animate it!

Thanks for all these answer :smile: !

I specify that I was not talking about the animator, but about the animation component.
I will try to profile it tonight, and have a conclusion about it.

thanks for your advices !

@Kurt-Dekker

Basically I think exactly like that, but I noticed that on low performance devices, having a lot of animated objects did not please certain devices too much…
Let’s imagine that the player leaves the objects on the ground, and that there are 15-20 animated objects in addition to the rest of the animations of the scene, it starts to be a lot to support for certain devices x)

Thanks for all again !

I suspect that animation happens entirely on the native engine side.

Not only that but that system is most likely highly optimized.

Anything you hack together on the C# scripting side will always have to “reach over” to the native side to do each step of its way.

Sure you might be able to do one particular thing faster than the stock animation system, maybe.

But you still have to maintain it, debug it, etc.

Definitely don’t think anectodally. If you have a performance issue than reach for the profiler and find out what is going on.

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://discussions.unity.com/t/841163/2

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://discussions.unity.com/t/846847/2

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.

1 Like

Using AnimationCurves and Curve.Evaluate() can be a quick way to do simple animations via code, and it has a bit of GUI to visualize things. Easy to make sine/“easing” curves and to loop them—just use Time.time() for the Evaluate() parameter, and you can scale it to any speed you want (Time.time() * 5 = 5X speed), speed up/slow down in response to events, etc. You can add keyframes via code as well, to change a given curve on the fly. Or (maybe more intuitive) just have a series of different curves that produce different results, then Evaluate() whichever one you want at that time.

1 Like