I’m running into an issue with AI character behavior in Unity. When I drag my AI prefab directly into the scene through the editor, everything works perfectly. However, when I instantiate the exact same prefab during gameplay using code, the AI doesn’t work properly.
The main problem is with the ThirdPersonCharacter component on my AI. The character gets stuck in idle animation and won’t transition to walking. I tracked it down to the ground detection system failing, specifically this part of the code:
characterAnimator.applyRootMotion = false;
This causes my character to slide across the ground instead of playing the proper walk animation. It seems like the ground detection isn’t working when the prefab is spawned at runtime, but I can’t figure out why the same prefab behaves differently depending on how it gets added to the scene.
I’m probably missing something obvious but I can’t spot the difference. Has anyone else encountered this kind of issue with Unity’s standard character controller?
your prefab’s probably spawning a bit above the ground, so the ground check fails. add a small delay after spawning, then force a ground check or just manually set the y position to fix the ground contact.
I’ve hit this transform parent issue before. Unity sometimes drops objects at Vector3.zero for a split second during instantiation before moving them to the target position.
Your ground detection raycast fires during that moment when the character’s floating in space, breaking the state machine. The animator gets confused about grounded vs airborne states.
Spawn your prefab with the parent parameter set explicitly and put the position in the same Instantiate call:
Had the same issue a few months ago - turns out it was a component initialization order problem. When you drop the prefab in the editor, everything initializes in the right sequence during scene load. But at runtime, ThirdPersonCharacter tries to cache ground detection references before colliders or the character controller are ready. Add a coroutine that waits one frame after spawning before enabling AI behavior. Just throw in a yield return null after instantiation, then enable your movement script. This gives Unity time to get all the physics components ready for ground detection. That root motion issue you’re seeing? It’s probably just a side effect of the ground check failing.