Addyarb

Untitled

Jan 13th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. using System;
  2. using Opsive.UltimateCharacterController.Camera;
  3. using Opsive.UltimateCharacterController.Character.MovementTypes;
  4. using Opsive.UltimateCharacterController.Utility.Builders;
  5. using Redcode.Awaiting;
  6. using SoftKitty.MasterCharacterCreator;
  7. using UnityEngine;
  8.  
  9. namespace Plantaria.PlayerSystem
  10. {
  11. /// <summary>
  12. /// Dynamically builds the character for use with Ultimate Character Controller and Master Character Creator.
  13. /// </summary>
  14. /// <remarks>
  15. /// There's a lot of hard-coded null/false arguments when invoking the builder methods.
  16. /// This is specifically for the Third Person Controller with minimal extras.
  17. /// If you need more features or wish to use the First Person perspective,
  18. /// you'll need to add them in by implementing more fields.
  19. ///
  20. /// Place this on the same GameObject as your CharacterEntity GameObject.
  21. /// Note: I added a property called InstantiationRoot to CharacterEntity.cs, which
  22. /// simply parents the instantiated prefab beneath a specified Transform instead of the root.
  23. /// </remarks>
  24. public class ThirdPersonCharacterRuntimeBuilder : MonoBehaviour
  25. {
  26. private GameObject _SpawnedCharacter => _characterEntity.InstantiationRoot.GetChild(0).gameObject;
  27.  
  28. [SerializeField] private CameraController _cameraController;
  29. [SerializeReference] private MovementType _movementType;
  30.  
  31. private CharacterEntity _characterEntity;
  32. private GameObject _character;
  33. private Animator _animator;
  34.  
  35. private void Reset()
  36. {
  37. _cameraController = FindFirstObjectByType<CameraController>();
  38. }
  39.  
  40. private void Awake()
  41. {
  42. if (!CanBuild()) throw new InvalidOperationException("Character cannot be built.");
  43.  
  44. _characterEntity = GetComponent<CharacterEntity>();
  45. _animator = GetComponent<Animator>();
  46. _character = this.gameObject;
  47. }
  48.  
  49. private bool CanBuild()
  50. {
  51. if (_cameraController.InitCharacterOnAwake)
  52. {
  53. Debug.LogError("Character should not be initialized on awake. This will be done at runtime after the character entity has been instantiated.");
  54. return false;
  55. }
  56.  
  57. return true;
  58. }
  59.  
  60. private async void Start()
  61. {
  62. // Wait until the character entity has been instantiated.
  63. // TODO: This should probably have a time-out
  64. await new WaitUntil(() => _SpawnedCharacter != null);
  65.  
  66. // Reference the animation controller in the Animator component. This is set by CharacterEntity.
  67. var animatorControllers = new[] { _animator.runtimeAnimatorController };
  68.  
  69. CharacterBuilder.BuildCharacter(
  70. character: _character,
  71. characterModels: new[] { _SpawnedCharacter },
  72. addAnimator: true,
  73. animatorControllers,
  74. firstPersonMovementType: string.Empty,
  75. thirdPersonMovementType: _movementType.GetType().FullName,
  76. startFirstPersonPerspective: false,
  77. thirdPersonObjects: Array.Empty<GameObject[]>(),
  78. invisibleShadowCasterMaterial: null,
  79. aiAgent: false
  80. );
  81.  
  82. TransferAnimatorToSpawnedCharacter();
  83.  
  84. CharacterBuilder.BuildCharacterComponents(
  85. character: _character,
  86. aiAgent: false,
  87. addItems: false,
  88. itemCollection: null,
  89. itemSetRule: null,
  90. firstPersonItems: false,
  91. addHealth: false,
  92. addUnityIK: true,
  93. addFootEffects: true,
  94. addStandardAbilities: true,
  95. addNavMeshAgent: false
  96. );
  97.  
  98. // The setter for this property calls InitializeCharacter, which will link the CameraController to the Character
  99. _cameraController.Character = _character;
  100. }
  101.  
  102. private void TransferAnimatorToSpawnedCharacter()
  103. {
  104. // Copy the existing Animator over to the newly spawned character prefab
  105. var newAnimator = _SpawnedCharacter.GetComponent<Animator>();
  106. newAnimator.avatar = _animator.avatar;
  107. newAnimator.runtimeAnimatorController = _animator.runtimeAnimatorController;
  108.  
  109. // Destroy the old Animator since it's no longer used
  110. Destroy(_animator);
  111. }
  112. }
  113. }
  114.  
Add Comment
Please, Sign In to add comment