0

In Unity, 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: (language - C#)

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: 1. 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.

2.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?

Tejaswini
  • 351
  • 3
  • 10
  • Why do you want to change the Animator during runtime? I suspect that the animator plays its animation when you change to it, overriding the rotation of your gameobject. But I don't see any reason why it would be necessary to change an animator during runtime. – Doh09 Nov 02 '17 at 09:39
  • It'a quite big project and animator controller already has many states. So we divided our project into separate independent modules, and we need animator controller to be changed runtime. – Tejaswini Nov 06 '17 at 04:28

1 Answers1

0

It;s resolved. It's a bug in Unity previous versions, but not in 2017.x. Everything is working fine in Unity 2017.x.

Tejaswini
  • 351
  • 3
  • 10
  • help me to solve **https://stackoverflow.com/questions/53117631/navmeshagent-player-not-parallel-to-slope-of-hill-when-moving-over-hill** – Night Programmer Nov 05 '18 at 09:24