public IEnumerator StartCountdown()
{
if (countdownActive)
yield break; // Don't start a new countdown if one is already active
countdownActive = true;
GameManager.Instance.countdownPage.SetActive(true);
startCountdownInt = 3;
UserInputs.inputREF.enabled = false;
status.text = "Get Ready";
while (startCountdownInt >= 0)
{
Debug.Log(startCountdownInt);
countdown.text = startCountdownInt.ToString();
yield return new WaitForSeconds(.3f);
startCountdownInt--;
}
timerActive = true;
UserInputs.inputREF.enabled = true;
GameManager.Instance.countdownPage.SetActive(false);
GameManager.Instance.gameUI.SetActive(true);
countdownActive = false; // Reset the flag when countdown is done
Debug.Log("Finished");
}
public class ResetPosition : MonoBehaviour
{
public Transform spawnPos;
// Update is called once per frame
void Update()
{
if (transform.position.y < -50f)
{
ResetPos();
}
if (UserInputs.inputREF.resetInput)
{
Debug.Log("RESETING");
ResetPos();
StartCoroutine(GameManager.Instance.timing.StartCountdown());
}
}
public void ResetPos()
{
Rigidbody rb = GetComponent<Rigidbody>();
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero; // Also reset angular velocity
// Temporarily disable physics
rb.isKinematic = true;
// Stop swinging logic
if (GetComponent<Swinging>().isActiveAndEnabled)
{
GetComponent<Swinging>().StopSwinging();
}
// Reset position using Rigidbody
rb.position = spawnPos.position;
// Reset rotation
rb.rotation = spawnPos.rotation;
// Reset camera rotation
GetComponent<PlayerMovement>().playerCamTransform.gameObject.transform.rotation = spawnPos.rotation;
// Re-enable physics
rb.isKinematic = false;
// Restart the timer
GameManager.Instance.RestartTimer();
}
}
In this script I am trying to call a coroutine to run a countdown when the player presses the reset button, this works fine when called by falling to far (First if statement) or at the start of a level but when the player presses a reset it gets stuck in a loop
This is the code for my input manager
void SetUpInputActions()
{
actions.Player.Enable();
actions.UI.Enable();
moveAction = actions.Player.WASD;
lookAction = actions.Player.Look;
jumpAction = actions.Player.Jump;
sprintAction = actions.Player.Sprint;
crouchAction = actions.Player.Crouch;
swingAction = actions.Player.Swing;
resetAction = actions.Player.Reset;
pauseAction = actions.Player.Pause;
UINextTab = actions.UI.NextTab;
UIPrevTab = actions.UI.PrevTab;
}
void UpdateInputs()
{
moveInput = moveAction.ReadValue<Vector2>();
lookInput = lookAction.ReadValue<Vector2>();
jumpFloat = jumpAction.ReadValue<float>();
crouchFloat = crouchAction.ReadValue<float>();
jumpInput = jumpAction.WasPressedThisFrame();
sprintInput = sprintAction.WasPressedThisFrame();
crouchInput = crouchAction.WasPressedThisFrame();
notCrouchInput = crouchAction.WasReleasedThisFrame();
swingInput = swingAction.WasPressedThisFrame();
notSwingInput = swingAction.WasReleasedThisFrame();
resetInput = resetAction.WasPressedThisFrame();
pauseInput = pauseAction.WasPressedThisFrame();
nextTabInput = UINextTab.WasPressedThisFrame();
prevTabInput = UIPrevTab.WasPressedThisFrame();
}
}
When the coroutine isn’t in there it only fires once. Any help appreciated