Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

2D texture timing issue when animator state is changed

Discussion in 'Animation' started by beggu84, Apr 8, 2021.

  1. beggu84

    beggu84

    Joined:
    May 6, 2014
    Posts:
    1
    Hi, I have some trouble with 2D sprite character.

    I want to animate sprite(SpriteRenderer) character with normal map.
    So, I replaced the Sprites-Default material with a new material using Legacy Shaders/Transparent/Bumped Diffuse.
    When I put the first sprite into the SpriteRenderer and put both a base texture and a normal map texture into the material and then play my scene with a point light, it works like this.
    upload_2021-4-8_17-28-3.png

    And the GameObject setting is like this.
    upload_2021-4-8_17-26-34.png

    As you can see, this character uses Animator with an animator controller and it has a couple of Animator State which has an animation clip. In each animation clip, related sprites are registered in it.

    So, everything is prepared for animating a sprite character with several motions, and in play mode, it animates well. Idle, run forward, backward, left, and right.

    The problem is, unlike sprites of SpriteRenderer, normal map property of the material has to be changed in manually for various motions. If not (a sprite is not matched with a normal map texture), it looks like this.
    upload_2021-4-8_17-50-1.png

    So, the the important thing is to find the perfect timing to change normal map when the character's animator state is changed.

    I tried this methods.
    1. When I call SetTrigger to change the animator state, find proper normal map and set it into the material. -> It changes textures before the animator state is changed.
    2. I add an event the first frame of each animation clip, so when the animator state is changed and the registered animation clip is played, registered event function is called and proper normal map is searched and set to the material. -> It changes textures too slow.
    3. In Update function, I get AnimatorStateInfo from the Animator and compared all animations' state name using stateInfo.IsName(...) to find normal map. -> It changes textures a little bit slow or fast about a frame?
    4. I add a custom StateMachineBehaviour into the 0 layer of Animator Controller. When OnStateEnter function is called, I find a proper normal map and set it to the material. -> This result is similar to number 3. Slightly slower or faster than sprite changing.

    And in each method, I also tried using Coroutine to make some delay. But it didn't work.

    Does anyone know when is the perfect timing for changing normal map texture according to the related sprites?

    Or, If I use legacy animation system, it works well?
     
    Last edited: Apr 8, 2021
unityunity