Search Unity

Question How can I make an idle animation only play when a sprite is on screen?

Discussion in 'Animation' started by Galaxykitte, Dec 20, 2022.

  1. Galaxykitte

    Galaxykitte

    Joined:
    Dec 20, 2022
    Posts:
    1
    I'd really appreciate help with this, I've been working on it for a while but I can't seem to get anywhere. I'm using sprites for the environment in my game, and I want plants to sway in the wind. I'm planning to do this by setting an idle animation, but I don't want it playing all the time since there are a lot of plants and that would cause a lot of lag. I've already made the animations, but I haven't gotten anywhere in the coding aspect.
    Also, I'm using bone animation for this, incase that changes anything.
     
  2. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    127
    I believe the "Culling Mode" in Animator may be what you want; if you set to "Cull Completely", the animator will totally stop when off-screen; note that if your animation moves a lot, it may go off-screen and won't come back unless it show on screen again.
    Also, maybe you can consider using shader instead of animator for swaying grass (if you or your team knows shader), it may be a lighter solution.
     
    Last edited: Dec 21, 2022
    Galaxykitte likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    Note that Culling mode is really hard to debug. Cull Completely means that the animator won't do anything when it's not visible to any camera. This includes the scene view camera! So it's not actually possible to see for yourself if an object is culling or not. You just have to count the number of things that show up in the profiler.
     
    Galaxykitte and Yuchen_Chang like this.