Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Opsive.UltimateCharacterController.Camera;
- using Opsive.UltimateCharacterController.Character.MovementTypes;
- using Opsive.UltimateCharacterController.Utility.Builders;
- using Redcode.Awaiting;
- using SoftKitty.MasterCharacterCreator;
- using UnityEngine;
- namespace Plantaria.PlayerSystem
- {
- /// <summary>
- /// Dynamically builds the character for use with Ultimate Character Controller and Master Character Creator.
- /// </summary>
- /// <remarks>
- /// There's a lot of hard-coded null/false arguments when invoking the builder methods.
- /// This is specifically for the Third Person Controller with minimal extras.
- /// If you need more features or wish to use the First Person perspective,
- /// you'll need to add them in by implementing more fields.
- ///
- /// Place this on the same GameObject as your CharacterEntity GameObject.
- /// Note: I added a property called InstantiationRoot to CharacterEntity.cs, which
- /// simply parents the instantiated prefab beneath a specified Transform instead of the root.
- /// </remarks>
- public class ThirdPersonCharacterRuntimeBuilder : MonoBehaviour
- {
- private GameObject _SpawnedCharacter => _characterEntity.InstantiationRoot.GetChild(0).gameObject;
- [SerializeField] private CameraController _cameraController;
- [SerializeReference] private MovementType _movementType;
- private CharacterEntity _characterEntity;
- private GameObject _character;
- private Animator _animator;
- private void Reset()
- {
- _cameraController = FindFirstObjectByType<CameraController>();
- }
- private void Awake()
- {
- if (!CanBuild()) throw new InvalidOperationException("Character cannot be built.");
- _characterEntity = GetComponent<CharacterEntity>();
- _animator = GetComponent<Animator>();
- _character = this.gameObject;
- }
- private bool CanBuild()
- {
- if (_cameraController.InitCharacterOnAwake)
- {
- Debug.LogError("Character should not be initialized on awake. This will be done at runtime after the character entity has been instantiated.");
- return false;
- }
- return true;
- }
- private async void Start()
- {
- // Wait until the character entity has been instantiated.
- // TODO: This should probably have a time-out
- await new WaitUntil(() => _SpawnedCharacter != null);
- // Reference the animation controller in the Animator component. This is set by CharacterEntity.
- var animatorControllers = new[] { _animator.runtimeAnimatorController };
- CharacterBuilder.BuildCharacter(
- character: _character,
- characterModels: new[] { _SpawnedCharacter },
- addAnimator: true,
- animatorControllers,
- firstPersonMovementType: string.Empty,
- thirdPersonMovementType: _movementType.GetType().FullName,
- startFirstPersonPerspective: false,
- thirdPersonObjects: Array.Empty<GameObject[]>(),
- invisibleShadowCasterMaterial: null,
- aiAgent: false
- );
- TransferAnimatorToSpawnedCharacter();
- CharacterBuilder.BuildCharacterComponents(
- character: _character,
- aiAgent: false,
- addItems: false,
- itemCollection: null,
- itemSetRule: null,
- firstPersonItems: false,
- addHealth: false,
- addUnityIK: true,
- addFootEffects: true,
- addStandardAbilities: true,
- addNavMeshAgent: false
- );
- // The setter for this property calls InitializeCharacter, which will link the CameraController to the Character
- _cameraController.Character = _character;
- }
- private void TransferAnimatorToSpawnedCharacter()
- {
- // Copy the existing Animator over to the newly spawned character prefab
- var newAnimator = _SpawnedCharacter.GetComponent<Animator>();
- newAnimator.avatar = _animator.avatar;
- newAnimator.runtimeAnimatorController = _animator.runtimeAnimatorController;
- // Destroy the old Animator since it's no longer used
- Destroy(_animator);
- }
- }
- }
Add Comment
Please, Sign In to add comment