L
L
LYTK42020-06-27 14:18:54
C++ / C#
LYTK4, 2020-06-27 14:18:54

How to make hp taken away with some delay?

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class HealthSystem : MonoBehaviour
{
  public int hp;
  	public Image[] lives;
  	public Sprite fullLive;
  public float transitionTime = 3f;
  	public Sprite emptyLive;
  private Enemy ensc;
  	public int numberOfLives;
    public GameObject dmg;
    public GameObject vs;
    Animator anim;

    void Start(){
      vs.SetActive(false);
    ensc = GetComponent<Enemy>();
    }

    void Update()
    {
    	if (hp > numberOfLives)
    	{
    		hp = numberOfLives;
    	}

       for (int i = 0; i < lives.Length; i++){
       	if (i < hp){
       		lives[i].sprite = fullLive;
       	} else {
       		lives[i].sprite = emptyLive;
       	}
       	if (i < numberOfLives){
       		lives[i].enabled = true;
       	} else {
       		lives[i].enabled = false;
       	}

    if (hp == 0) {
      SceneManager.LoadScene("Menu");
    }
       }

    }
    void OnTriggerStay2D(Collider2D col){
    	if (col.CompareTag("EnemyDefolt")){
      StartCoroutine(ToWait());
      hp = hp - 1;
    	}

  IEnumerator ToWait()
  {
    yield return new WaitForSeconds(10f);
  } 
 
    }
}
HP is taken away very quickly, in less than a second, and it is necessary that after taking one hp there was a delay and damage was done again.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-06-27
@firedragon

Set the delay parameter, cock it when taken away, see the difference when repeating

S
SagePtr, 2020-06-27
@SagePtr

It is possible, as in arcade games, when taking damage, make the character invulnerable for a few seconds

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question