using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerManager : MonoBehaviour { public GameObject playerGameObj; public GameObject PlayerMainCamera; public GameObject playerHead; public CharacterController PlayerCharacterController; [Tooltip("FPS Controller Animator is not the animator that animates the character but the animator for the FPS Controller")] public Animator FPSControllerAnimator; public PlayerMovement _PlayerMovement; public CameraMovement _cameraMovement; public Sanity playerSanity; public bool playerMovementCanBeEnabledIfDisabled = true; private Coroutine PlayerAnimationSmoothTransitionFromWalkToIdleCoroutine; private static PlayerManager _instance; public static PlayerManager GetInstance() { return _instance; } void Awake() { if (!_instance) { _instance = this; } // Find the GameObject with the specified tag in the editor GameObject foundPlayer = GameObject.FindGameObjectWithTag("Player"); // Check if a GameObject with the tag was found if (foundPlayer != null) { // Assign the found GameObject to the playerObject field playerGameObj = foundPlayer; } else { Debug.LogError("No GameObject with tag " + "Player" + " found in the scene."); } // Find the GameObject with the specified tag in the editor GameObject foundPlayerMainCamera = GameObject.FindGameObjectWithTag("MainCamera"); // Check if a GameObject with the tag was found if (foundPlayerMainCamera != null) { // Assign the found GameObject to the playerObject field PlayerMainCamera = foundPlayerMainCamera; } else { Debug.LogError("No GameObject with tag " + "MainCamera" + " found in the scene."); } PlayerCharacterController = playerGameObj.GetComponent(); _PlayerMovement = playerGameObj.GetComponent(); _cameraMovement = playerGameObj.GetComponentInChildren(); if(FPSControllerAnimator == null) { FPSControllerAnimator = playerGameObj.GetComponent(); } } private void OnValidate() { //// Find the GameObject with the specified tag in the editor //GameObject foundPlayer = GameObject.FindGameObjectWithTag("Player"); //// Check if a GameObject with the tag was found //if (foundPlayer != null) //{ // // Assign the found GameObject to the playerObject field // playerGameObj = foundPlayer; //} //else //{ // Debug.LogError("No GameObject with tag " + "Player" + " found in the scene."); //} //// Find the GameObject with the specified tag in the editor //GameObject foundPlayerMainCamera = GameObject.FindGameObjectWithTag("MainCamera"); //// Check if a GameObject with the tag was found //if (foundPlayerMainCamera != null) //{ // // Assign the found GameObject to the playerObject field // PlayerMainCamera = foundPlayerMainCamera; //} //else //{ // Debug.LogError("No GameObject with tag " + "MainCamera" + " found in the scene."); //} } public void DisablePlayerMovement() { PlayerAnimationSmoothTransitionFromWalkToIdleCoroutine = StartCoroutine(PlayerAnimationSmoothTransitionFromWalkToIdle()); _PlayerMovement._isRunning = false; _PlayerMovement.isMoving = false; _PlayerMovement.isWalking = false; _PlayerMovement.enabled = false; PlayerCharacterController.enabled = false; } IEnumerator PlayerAnimationSmoothTransitionFromWalkToIdle() { while (_PlayerMovement._CharacterAnimator.GetFloat("speedPercent") > 0.01) { _PlayerMovement._CharacterAnimator.SetFloat("speedPercent", 0, 0.2f, Time.deltaTime); //Sets the transition to idle smoothly. _PlayerMovement._isRunning = false; _PlayerMovement.isMoving = false; _PlayerMovement.isWalking = false; print("Player speed percent for animator is being smoothly transitioning"); yield return null; } print("Player movement has been smoothly transitioned from walk to idle (I hope so)"); } public void EnablePlayerMovement() { if (playerMovementCanBeEnabledIfDisabled) { _PlayerMovement.enabled = true; PlayerCharacterController.enabled = true; if (PlayerAnimationSmoothTransitionFromWalkToIdleCoroutine != null) { StopCoroutine(PlayerAnimationSmoothTransitionFromWalkToIdleCoroutine); } //PlayerAnimationSmoothTransitionFromWalkToIdleCoroutine = null; } } public void DisablePlayerCameraMovement() { _cameraMovement.enabled = false; } public void EnablePlayerCameraMovement() { _cameraMovement.enabled = true; } public void PlayerMovementCannotBeEnabled() { playerMovementCanBeEnabledIfDisabled = false; } public void PlayerMovementCanBeEnabled() { playerMovementCanBeEnabledIfDisabled = true; } public void UseCameraBedHideMovement() { _cameraMovement.UseCameraBedHideMovement(); } public void UseRegularGameplayCamera() { _cameraMovement.UseRegularGameplayCamera(); } }