Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Arthur_Gentz · Oct 09, 2021 at 11:02 AM · animator controlleranimationstate

How to force an animation state in an animator controller to go to the exact play percentage of an animation state of equal length in a different animator controller

So i've been struggling all day trying to get an animation i made in blender to work in unity. The hoe is constrained to an armature (call it hoe_to_hands_armature), and the hands are also constrained to this very same armature. The character has his own armature (call it player_armature), which totals two armatures for the animation. Note that the armatures contain the animations. That is, the hoe_to_hands_armature "contains" the animation that move this hoe and hands about, creating the hoe movements in the animation. With the player_armature "containing" the animation that move the player about. I imported the player model and player_armature into unity, then did the same for the hoe model and hoe_to_hands armature. I then made an empty game object in the unity scene, and made the player_armature and hoe_to_hands_armature children of said empty. I also added separate animation controllers and avatars to both the player_armature, and the hoe_to_hands_armature. The animation clip of both armatures are the exact same length. The first loop of the animation works perfectly, successive loops see the player_armature progressively "falling behind" the hoe_to_hands_armature. So my question is, how do i force either animation state in the two separate anim controllers to "go" to the playtime/progression of the the other? Demo

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Arthur_Gentz · Oct 09, 2021 at 11:37 AM

Found a solution, by just comparing the 'percent completion' of one animation state with that of the other. If the difference between 'percent completions' is large enough, i simply play one of the two while using the normalized time of the other.

 using System;
 using UnityEngine;
 
 public class SyncPlaybackTimes : MonoBehaviour
 {
     public string playerAnimStateName;
     public string propAnimStateName;
     
     [SerializeField] private Animator _playerAnimator;
     [SerializeField] private Animator _propAnimator;
     
     private AnimatorStateInfo _playerAnimStateInfo;
     private AnimatorStateInfo _propAnimStateInfo;
     
     private void LateUpdate()
     {
         SyncPropWithCharAnim();
     }
 
     private void SyncPropWithCharAnim()
     {
         _propAnimStateInfo = _playerAnimator.GetCurrentAnimatorStateInfo(0);
         float charAnimCompletion = PlaybackPercent(_propAnimStateInfo);
         
         _playerAnimStateInfo = _propAnimator.GetCurrentAnimatorStateInfo(0);
         float propAnimCompletion = PlaybackPercent(_playerAnimStateInfo);
 
         float syncDiff = propAnimCompletion - charAnimCompletion;
         bool synced = Math.Abs(syncDiff) < 0.01f;
         if ( synced ) return;
 
         //Both the character and prop animations are the same length and ought to have started at the exact moment. So if the prop animation goes out of sync, simply re-sync by (re)playing the prop animation but started at the normalized time (progress) of the character animation
         _propAnimator.Play(propAnimStateName, 0, _playerAnimStateInfo.normalizedTime);
     }
 
     public float PlaybackPercent(AnimatorStateInfo animationStateInfo)
     {
         float normTime = animationStateInfo.normalizedTime;
         float percentComplete = normTime - Mathf.Floor(normTime);
         return percentComplete;
     }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arthur_Gentz · Oct 09, 2021 at 11:38 AM 0
Share

If anyone knows of a simpler less hacky solution, please share.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I cannot switch the current animation state to another state immediately. 0 Answers

Mecanim, get state percent complete when a transition occurs? 0 Answers

Animation State of Controller not showing in Inspector when clicked 10 Answers

Animator Override controller SubstateMachine issue 0 Answers

Animation play not working C# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges