Sync Animation Between Objects

I’m not sure if I’m going about this the wrong way, but I’m trying to get two objects with the same animation to play at the same time.

I have a character with a separate clothing mesh. Character and clothing exported to separate fbx files, but both use the same skeleton. I can tie the character animation to the clothing and it does animate correctly. I have an animation script similar to the character that should animate the clothing when the character walks. They both animate, but not quite at the same time. The clothing animation seems to be a few frames ahead or behind the character animation so that the clothing doesn’t match up to the character as it moves.

I’m not sure if someone else has done something similar or if maybe there is a better way to do this. I wanted the clothing meshes separate to more easily expand clothing options over time instead of having it all tied to the same model and having to re-create the character prefab every time I added more clothing options.

Thanks!

Are you calling Animation.Stop in the Start or Awake function and manually starting the animations at the same time? Are they playing at the same speed, i.e. is the cloth always X frames out of synch with the character? Do you have a single “animation handler” that sends a single message to both objects, such as when you would crossfade from Walk to Run, ect? If they are playing at the same speed and perpetually out of phase by the same degree then maybe you have a race condition in how they start up; if they seem to drift in and out of phase then I think you have 2 peices of asynch code that might need to be mustered in a single animation handler?

Just speculating.

Charles

The clothing is playing at the same speed as the character animation and is always offset in playing by the same amount. It’s as if it’s not starting the animation from the first frame.

I have tried two methods.

  1. When the character plays an animation it broadcasts a message down the chain to the clothing to play the same animation.

  2. The character and the clothing have similar mechanisms for animation (based on the third person tutorial animation scripts), but the character passes it’s speed down to the clothing which in turns processes the animation that should be played.

Both methods yield the same results with the clothing animation being offset by a certain amount. The clothing I’m using for the example are pants and they animate almost opposite the character meaning that when the right leg of the character is forward the right leg of the pants is back. The pants are using the animation that was imported with the character so they are sharing the same animation data. I’m not sure why this is occurring.

OK… not quite there but seemed to figure something out. If I make sure to rewind animations after they are used then the animations start flowing into sync. So if I walk the first time now it’s offset and then when I switch back to idle the script rewinds the walk animation so that now when I walk it’s almost perfectly in sync. The problem here is that it isn’t animated correctly the first time and creates a weird glitch as I crossfade back to idle.

Managed to get things more on track by attempting to sync the animationState time between the two running animations. What is strange though is that when it loops there is a minor overlap of the clothing and the character mesh. The rest of the animation syncs perfectly, but for some reason when it resets to 0 the clothing offsets for a single frame and then is back on track.

I’m thinking the other way of doing this is having the clothing built into the character model and hiding/unhiding the clothing as needed. Was not hoping to do it this way, but can’t seem to get the animations synced any other way.

Is your animation clip set to “Loop” on the asset import option? This will add an additional frame at the end of the animation to ensure the wrapmode looks clean.

You mentioned before about “stopping” and “starting” the animations, and maybe I misunderstand, but your idle / walk / run animations are all set to WrapMode, correct, and the each play constantly in the same layer, and the animation clips are imported with the Loop option set and they are all in the same layer with SyncLayer being called after they have been loaded?

Sorry for so many questions;

Charles

Based on how you described it a few posts up I would suggest stripping the logic down to the bare minimum and do a proof of concept, meaning eliminate all the fancy stuff and set the model so a single, looping walk animation; if that works try a crossfade to your idle animation in the same layer. Perhaps this is what you are already doing.

I had already stripped down my code to working with just a walk and idle animation. I removed the blending code and was just using animation.Play to attempt to resolve any issues. Both animationStates are set to loop. I’m using the same imported animation for both the character and the pants so I’m unsure why I’m getting the hiccup I described above.

I also removed any layers from the animation clips and was just trying to sync the animationState.time between the two objects which almost worked.

I have deviated a bit from my initial thought and attempted a drastically different approach. I started a clean project to test a new thought I had which was:

  1. Create a new gameobject under the character prefab

  2. Via startup script attach a SkinnedMeshRenderer to this new game object. Also set the number of bones to be the same as the number bones in the character.

  3. Set a button so that on click it makes the sharedMesh of the skinnedMeshRenderer to that of the clothing. In this case the pants.

Now right away this red flagged with “Number of bind poses doesn’t match number of bones in skinned mesh”. After some research and head banging I realized (and correct me if I’m wrong) that bindposes are just a reference to the bones and what the initial transformation is for said bone and that the number of bindposes reference via it’s key only the number of bones that have influence over the mesh.

My thinking was that the bindposes and bones were all the imported bones of the object, but that does not seem to be the case. Only those that have influence over the mesh vertices are added to the bone/bindpose listing.

So this theory is not working as I planned. Back to drawing board.