- Home /
Transform Of GameObject getting changed incorrectly after updating RuntimeAnimatorController
A gameObject has NavMeshAgent and Animator component.
I am changing runTimeAnimatorController of Animator component in a function called 'changeAnimatorController' at run time in the following way:
RuntimeAnimatorController newAnimController = Resources.Load("AnimControllers/WalkAnimatorController") as RuntimeAnimatorController;
void changeAnimatorController()
{
this.gameObject.GetComponent<Animator>().runtimeAnimatorController = newAnimController ;
}
Now calling this function is working differently when called in the following two different ways:
If I call the function 'changeAnimatorController' directly from script, my Animator controller is getting updated to the new one, but gameObject's position is getting changed to the gameObject's original position present before the game play starts, and getting updated to the correct position in next frame. For example, before the game play starts, my gameObject is at position (0,0,0). After game starts, at a particular time my gameObject moved to (10,0,0) through animation. Now if I call the function 'changeAnimatorController', gameObject's position is getting changed to (0,0,0) , but again getting updated to (10,0,0) in the next frame.
If I call the function 'changeAnimatorController' from an event of animation clip present in current animator controller (which I am going to replace), gameObject's position is getting changed to the gameObject's original position present before the game play starts, and never getting updated to the correct value in any frame.
For example, before the game play starts, my gameObject is at position (0,0,0). After game starts, at a particular time my gameObject moved to (10,0,0) through animation. Now if I call the function 'changeAnimatorController' from an event of animation clip, gameObject's position is getting changed to (0,0,0) and never getting back to (10,0,0). But to my surprise here, if baseOffset of gameObject's navMeshAgent is updated immediately to some value after updating RunTimeAnimatorController in the following way, it's position is getting updated to the correct value ((10,0,0) in the above example).
void changeAnimatorController()
{
this.gameObject.GetComponent<Animator>().runtimeAnimatorController = newAnimController ;
this.gameObject.GetComponent<NavMeshAgent>().baseOffset = requiredBaseOffset;
}
In all the above cases, after changing RunTimeAnimatorController of gameObject, rotation of gameObject is getting changed to gameObject's original rotation present before game play starts. For example, before the game play starts, my gameObject's rotation is (0,0,0). After game starts, at a particular time my gameObject's rotation is (120,0,0) through animating. Now if I call the function 'changeAnimatorController', gameObject's rotation is getting changed to (0,0,0) and never getting back to (120,0,0).
Any idea why it's happening this way? Why calling the function 'changeAnimatorController' from different places works differently? What's the correct way to change runTimeAnimatorController of Animator at run time?
Your answer
Follow this Question
Related Questions
How to prevent an animation from triggering more than on due to fast clicking? 2 Answers
StateMachineBehaviour is working weird with AnimationClip.AddEvent 0 Answers
AnimationEvent has no receiver, but there is no AnimationEvent 1 Answer
How do i add an AnimationEvent to an animation of a single instance with script? 0 Answers
Animation Event is won't be called 0 Answers