Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with mecanim and syncing animations for two 2d objects!

Discussion in '2D' started by esco1979, May 12, 2014.

  1. esco1979

    esco1979

    Joined:
    Mar 18, 2014
    Posts:
    136
    Hey everyone, I managed to solved my old issue but am having a problem getting my animations to perfectly sync up.

    Let me explain: I have a seperate player object (called alucard) and a seperate weapon object. I setup the player so when a button is hit he goes into an attack animation and the weapon is also created. That is the only way the player effects the weapon, by creating it and setting the initial position.

    The weapon itself has its own mecanim animation that is the EXACT same length as the player's attack animation; both are 30 steps long, and sample is set to 60. At the end of the animation the weapon is destroyed by an event on the timeline.

    I noticed that when doing this that the weapon's animation did not perfectly match the player's. So I put this under the weapon's script:
    Code (csharp):
    1. animator.ForceStateNormalizedTime(alucard_animator.GetCurrentAnimatorStateInfo(0).normalizedTime)
    In an attempt to make the weapon's timer perfectly match the players! I tried putting this under all 3 updates, and even doing it from the player object, but no matter what I do the number still seems to be off for the weapon and the animation is behind by one frame generally. I even tried adding it via a script event in mecanim, same issue. I get no errors, am I doing something wrong from a syntax perspective? Any useful help here would be REALLY appreciated as I am at my wits end with mecanim.

    I also heard of something called sync layers but could find no info on them other than the next to useless info on it in the documentation. Would those help here? If so can someone point me towards using them for 2d objects?
     
    Last edited: May 13, 2014
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    This is going to sound flippant, but if it's always off by one frame, maybe add an empty frame? 1/30th of a second can be enough to make synced animations look awful, but shouldn't affect gameplay too much.
     
  3. esco1979

    esco1979

    Joined:
    Mar 18, 2014
    Posts:
    136
    What the? Really, for everything? I figured I must have been doing something wrong. Why is that?

    And effecting animations is a BIG deal, even if it doesn't effect gameplay. That's pretty bad for a program that costs over a grand to have an issue like that for something so basic. :(
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    There is no issue with syncing animations. The problem is you are starting an animation and creating the second object at the same time. So you aren't actually starting them at the same time. Best case scenario is that the weapon will be one frame behind, but it could be worse depending on other factors. If you want them to play at the same time, make sure the weapon is already in place then start them both. or make the weapon a specific amount of time shorter, and trigger it at that time from the main timeline, allow for enough time to be in place.
     
  5. esco1979

    esco1979

    Joined:
    Mar 18, 2014
    Posts:
    136
    What do you mean trigger it from the main timeline? And if I am creating the object shouldn't its defailt timeline start at the same time?

    Also I'm not sure that what you are saying is correct: I STILL have a statement above that is copying the current time from the main timeline to the weapon every step so they should still be the same. So why aren't they?
     
  6. esco1979

    esco1979

    Joined:
    Mar 18, 2014
    Posts:
    136
    Ok, so after messing around with the sword as a seperate object, and realizing that due to the fact that when mecanim changes states there is yet ANOTHER 1 frame delay ( meaning if I use a crouch attack instead of a standing one that for 1 step you can see the sword would be in the wrong position on screen, and yes it is VERY noticeable), and that I would have to make seperate animators for each type of attack the player can use with the sword (6 total), I decided to set the sword object to AGAIN be a child of the ALUCARD object.

    I then setup an incrementer that ran every late update, and based on this would then set the sprite for my weapon. This was done via script and easy to do, but the issue was that sometimes the animation would STILL get thrown off by a step, and othertimes it wouldn't.

    So next I setup that same script script to run via an event in the mecanim animation; this script handled the incrementer and picked the sprites for the sword based on its value. The same issue arises; sometimes it syncs perfect with the player, other times the sword is off by a step.

    NEXT, I actually took out my incrementer, and just setup a bunch of seperate scripts (13 total with just a swtch case statement, called frame0 - frame13). I then would just run each script via an event on the mecanim timeline when needed. And GUESS WHAT? It works perfect!!!!

    BUT IT MAKES NO GODDAMN SENSE! Why did it not work with an incrementer? And based on the event execution order listed in the documentation any of the MULTITUDE of things i tried previously should have worked! So why didn't they? Does Mecanim operate seperately from Unity?

    This is REALLY starting to piss me off here. Something this simple shouldn't have been this hard to get working! And now I feel even more confused. Can anyone explain to me what is going on here? I am willing to provide my project if needed as it is still small and basic.
     
    Last edited: May 13, 2014
  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    Not really, it will start playing as soon as it is available. If you are adding to the scene (I am assuming instantiating it) it will take at least a frame. (maybe more depending on size and awake/start/enable events).
    I was just saying that one option would be have have the character use a timeline event to start the other animation. So the sword could be .2 seconds shorter, but on the character timeline it calls play on the on the sword at .2 seconds into its time line. Sort of a manual sync.

    Dunno, that could be making it worse, I don't think that function is intended to be used like that (on every frame). Additional overhead typically will affect the timing of animations, just not in a good way. ;)
     
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    You definitely don't want to do that, especially if you are concerned about timing.

    I think you are over thinking/complicating things. Each additional thing you add to the process has the potential to knock the timing off a little bit, especially since they are separate controllers and animations. "Frames" aren't fixed, and since the animations are all separate, it is possible for them to get a tiny bit out of sync, even just the complexity of the contents can effect it. If really want to ensure that things are in sync, combine them. One animator can control a nested animator. Or even just have it as a layer inside. Everything under the control of a single animator will be in perfect sync. Usually individual ones work fine as well, all things being equal, but if they can shift a bit. If they are meant to be part of the same animation (a guy swinging a sword for example), they should be under the same anim/controller. I think you will find it hard to sync up individual animations that are not part of the same animator, at least to frame level perfection.