Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
¿Cómo hacer que mi resistencia se regenere lentamente?

Necesito ayuda con mi proyecto de juego móvil de mundo abierto. Tengo un jugador que puede caminar y correr cuando presionamos un botón. Hice una rueda de resistencia (como en zelda abajo), y cuando mi jugador corre, la resistencia disminuye. También he puesto una rutina de regeneración que hace que la resistencia se regenere. Pero debido a que mi maxStamina = 1, el tiempo entre que está vacío y lleno es muy rápido. Aquí está mi código:

 public static StaminaUI instance; private WaitForSeconds regenTick = new WaitForSeconds(0.1f); private Coroutine regen; private void Awake() { instance = this; } void Start() { fillAmount = maxFill; } void Update() { if (Ybot.MoveSpeed > 5f) { UseStamina(0.015f); } if (fillAmount < 0.01f) { Ybot.MoveSpeed = 0f; Ybot.animator.SetBool("isRunning", false); Ybot.animator.SetBool("isWalking", false); } } public void UseStamina(float amount) { if (fillAmount - amount >= 0) { fillAmount -= amount; if (regen != null) { StopCoroutine(regen); } regen = StartCoroutine(RegenStamina()); } else { Debug.Log("Not enough stamina"); } } private IEnumerator RegenStamina() { yield return new WaitForSeconds(1); while (fillAmount < maxFill) { fillAmount += maxFill/1; yield return regenTick; } regen = null; }

Espero que alguien me pueda ayudar con este pequeño problema.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Dado que incrementa cada 0.1 segundos, creo que debería ser

 fillAmount += maxFill / desiredDuration * 0.1f;

donde la desiredDuration es el tiempo en segundos necesarios para llenar completamente la resistencia si es 0 .


O como alternativa para actualizaciones fluidas en lugar de los pasos de 0.1 segundos que podría hacer

 while (fillAmount < maxFill) { fillAmount += maxFill / desiredDuration * Time.deltaTime; yield return null; }
over 4 years ago · Santiago Trujillo Report

0

Debe usar Time.time y luego hacer la regeneración si TIme.time es> que un intervalo fijo. Debe darse cuenta de que Update se llama una vez por cuadro. Esto podría significar que se puede llamar entre 30 y 90 veces por segundo, según la frecuencia de actualización de su dispositivo.

La actualización se llama una vez por cuadro, por lo que en su código debe hacer algo similar a esto:

lastRegenTime y regenIntervals pueden ser flotantes (lo que puede ser costoso), pero en Actualizar la lógica sería ->

 if (Time.time - lastRegenTime > regenInterval) { Regenerate(); }

Establezca regenInterval en algo bajo y luego configure el incrementador para la vida en life+=0.02f; Incluso puede hacerlo de forma incremental, o *=0.00002f;

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!