Standard prefab for both main and ai characters

Pedro

Member
I've been trying to create a standard prefab for all characters to use and then initiate that prefab at runtime and modify the attached scripts to make it either the main character controller or an ai character (in the future it could also be a network character). I must be missing something cause when I set up the AI character I get navmesh errors even though it has a navmeshagent attached and a behavior tree.

Is there any documentation on how to attache/activate the required scripts at runtime and set them up to turn a main character into an ai character or vice versa? I understand how to do it if I drag the prefab in and set it up manually, but I want something that can be done dynamically when the game starts.

Is this possible and is it documented anywhere?

Thanks
 
This post should help :)

 
I was able to use that post as a base to get this working. However, when instantiating a UMA character (with a bone structure), I get the following error:

Error: The CharacterIK component only works with humanoid models. UnityEngine.Debug:LogError(Object) Opsive.UltimateCharacterController.Character.CharacterIK:Awake() (at Assets/Opsive/UltimateCharacterController/Scripts/Character/CharacterIK.cs:221)

It doesn't seem to have any negative effect though and everything works.

One important thing I found missing from the above mentioned code when using with a behavior tree AI is that the NavMeshAgent ability would need to be added to the character controller as disabled and then enabled for a behavior tree AI. I do something like:

Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion controller = spawnedCharacter.GetComponent<Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion>(); var ability = controller.GetAbility<Opsive.UltimateCharacterController.Character.Abilities.AI.NavMeshAgentMovement>(); ability.Enabled = true;

In case anyone reaches this post so they know to do that.
 
The CharacterIK error is causing more problems than I initially noticed since it blocks many abilities from working. It seems what's happening is that when the character is instantiated from the prefab, CharacterIK is calling the following code before the animator recognises the object as humanoid.

C#:
            // Assign the humanoid limbs. If the character is not a humanoid then the component will stay disabled because Unity's IK system
            // only works with humanoids.
            if (!m_Animator.isHuman) {
                Debug.LogError("Error: The CharacterIK component only works with humanoid models.");
                m_Enable = enabled = false;
                return;
            }

I've tried removing the CharacterIK script and adding it with code (gameObject.AddComponent) but since other code from the character controller script depends on it and is being called before I add the CharacterIK it throws a lot more error messages.
 
Are you sure that your character model is humanoid? If the Animator is reporting that it's not humanoid but it should be then I'd check the import settings for your model. I have a test scene which spawns the character from a prefab so I know that works. Here's the test script that I use (though it's pretty basic)

Code:
    private void Create()
    {
        var character = GameObject.Instantiate(m_CharacterPrefab);
        var camera = GameObject.Instantiate(m_CameraPrefab);

        // The character needs to be assigned to the camera.
        var cameraController = camera.GetComponent<CameraController>();
        cameraController.Character = character;

        // The virtual controls need to be updated.
        var virtualControls = m_VirtualControls.GetComponent<VirtualControlsManager>();
        if (virtualControls != null) {
            virtualControls.Character = character;
        }
    }
 
The character is a humanoid but it's using UMA and UMA takes a while to create the character Avatar for the Animator (even with the skeleton pre-made). I ran some testing attaching a script that monitors the .isHuman of the character and it could take up to half a second in some cases. Because of this, when CharacterIK starts it's still seeing it as non-humanoid.

When I drag the exact same character prefab into the scene and start it "normally" rather than instantiating it via code I don't get the CharacterIK issues.

I was thinking of trying the script UMA Character Builder you mention in the documentation at: https://opsive.com/support/documentation/ultimate-character-controller/integrations/uma/ to see if loading the character controller after UMA is started solves this issue but I couldn't find the script in the Character Controller package I have. Do I have to download it separately?
 
I managed to sort of fix it by using some of the code from: https://www.opsive.com/forum/index.php?threads/uma-2-howto.14/

However, that didn't work on its own, I had to remove the CharacterIK script from the UMA prefab entirely and add it using AddComponent only after the SetUMAReady call back if ready=true.

That's because even if the script is disabled the Awake function is still called on loading it and that function is where the isHuman test takes place before UMA has created the humanoid avatar for the animator.

Now I need to test if the IK actually works for the abilities that require it...
 
I was thinking of trying the script UMA Character Builder you mention in the documentation at: https://opsive.com/support/documentation/ultimate-character-controller/integrations/uma/ to see if loading the character controller after UMA is started solves this issue but I couldn't find the script in the Character Controller package I have. Do I have to download it separately?
Ah, yes, with UMA you'll need to use the UMA Character Builder. On that page there is a "Downloads" section which contains the UMA integration for the controller that you are using. I believe that you are on TPC? This link will work:

 
Ah, yes, with UMA you'll need to use the UMA Character Builder. On that page there is a "Downloads" section which contains the UMA integration for the controller that you are using. I believe that you are on TPC? This link will work:

Nice, it has helped me too. However, it is UMA Bone Builder that you need to run once again, not UMA Character Builder.
 
Top