Search Unity

Question How to code in a wait period between an action

Discussion in 'Physics' started by Polarfist1, Oct 24, 2021.

  1. Polarfist1

    Polarfist1

    Joined:
    Aug 16, 2021
    Posts:
    15
    Hi, In my game, the player has 100 health. If an enemy touches the player, the health goes down by 10. However, there is not wait period between the action (Im using OnCollisionStay) and so the health goes down nearly at an instant when the enemy is colliding with the player. That is why I want a wait period between the action (one second) so when the enemy is touching the player, the health of the player goes down 10 each second, instead of 10 each frame. I've tried searching for an answer for hours but still no luck.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class Enemy : MonoBehaviour
    5. {
    6.     public float health = 100f;
    7.     GameController spawn;
    8.     GameObject SpawnPointManager;
    9.     GameObject playerParent;
    10.     PlayerMovement playerScript;
    11.     public int damageAmount = 10;
    12.  
    13.     private void Start()
    14.     {
    15.         SpawnPointManager = GameObject.FindGameObjectWithTag("spawnPointManager");
    16.         spawn = SpawnPointManager.GetComponentInChildren<GameController>();
    17.         playerParent = GameObject.FindGameObjectWithTag("firstPersonPlayer");
    18.         playerScript = playerParent.GetComponent<PlayerMovement>();
    19.     }
    20.  
    21.     public void TakeDamage (float amount)
    22.     {
    23.         health -= amount;
    24.         if (health <= 0f)
    25.         {
    26.             Die();
    27.         }
    28.     }
    29.  
    30.     void Die ()
    31.     {
    32.         FindObjectOfType<MoneyScript>().GetMoneyFromEnemy();
    33.         FindObjectOfType<ScoreScript>().OnEnemyKilled();
    34.         spawn.enemiesKilled++;
    35.         Destroy(gameObject);
    36.     }
    37.  
    38.     private void OnCollisionStay(Collision collision)
    39.     {
    40.         if(collision.gameObject.tag == "Player")
    41.         {
    42.            
    43.             if(playerScript.health > 0)
    44.             {
    45.                 damagePlayer();
    46.             }
    47.            
    48.  
    49.         }
    50.     }
    51.  
    52.     private void damagePlayer()
    53.     {
    54.         playerScript.health -= damageAmount;
    55.     }
    56. }
    57.  
     
  2. bittype

    bittype

    Joined:
    Oct 5, 2021
    Posts:
    1
    1. float ts = 0.0f;
    2. private void OnCollisionStay(Collision collision)
    3. {
    4. if(collision.gameObject.tag == "Player")
    5. {
    6. if(playerScript.health > 0)
    7. {
    8. if (ts < Time.timeSinceLevelLoad)
    9. {
    10. ts = Time.timeSinceLevelLoad + 1;
    11. damagePlayer();
    12. }
    13. }

    14. }
    15. }
     
  3. Polarfist1

    Polarfist1

    Joined:
    Aug 16, 2021
    Posts:
    15
    Thanks a lot, it worked