How to have a poison effect
Hi there
How's everyone?
well, I'll get to the case, you see I'm making a 2D adventure game and I want to have a poison status effect on my character but I can't get a good one and I looked all over in internet and none made the poison damage sensible.
here's a video of what's going on in the game:
https://www.mediafire.com/file/9hg39rxyxp385b3/2018-11-01_16-18-36.flv/file
and here's the code I made (you'll notice more than one code about the poison damage cuz I took it from different sites and I need you help figuring out the mistakes I made):
public float maxHealth;
public GameObject deathFX;
public AudioClip playerHurt;
public float currentHealth;
PlayerController controlMovement;
AudioSource playerAS;
public Slider healthBar;
public Image damageScreen;
bool damaged = false;
Color damagedColor = new Color(0f, 0f, 0f, .5f);
float smoothColor = 5f;
/*public bool isPoisoned;
public bool poisonEffect;
public int poisonDamage;
public float poisonTimer;*/
public float HowLongPoisonLasts;
public float PoisonDamageInterval;
float nextPoisonDamage;
public float poisonDamage;
public bool isPoisoned;
public bool poisonEffect = true;
// Use this for initialization
void Start () {
//isPoisoned = false;
currentHealth = maxHealth;
controlMovement = GetComponent<PlayerController>();
healthBar.maxValue = maxHealth;
healthBar.value = maxHealth;
damaged = false;
playerAS = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
if (damaged)
{
damageScreen.color = damagedColor;
}
else
{
damageScreen.color = Color.Lerp(damageScreen.color, Color.clear, smoothColor * Time.deltaTime);
}
damaged = false;
if (isPoisoned)
{
StartCoroutine("PoisonDamage");
}
if (currentHealth <= 0)
Die();
}
public void TakeDamage(float damage)
{
if (damage <= 0)
return;
currentHealth -= damage;
playerAS.clip = playerHurt;
playerAS.Play();
isPoisoned = true;
//playerAS.PlayOneShot(playerHurt);
healthBar.value = currentHealth;
damaged = true;
if (currentHealth <= 0)
{
Die();
}
}
public void Die()
{
Instantiate(deathFX, transform.position, transform.rotation);
Destroy(gameObject);
}
IEnumerator PoisonDamage()
{
float poisonCounter = 0;
while (poisonCounter < HowLongPoisonLasts)
{
/*isPoisoned = true;
currentHealth -= poisonDamage * Time.deltaTime;
healthBar.value = currentHealth;
yield return new WaitForSeconds(PoisonDamageInterval);
poisonCounter += PoisonDamageInterval;*/
currentHealth -= poisonDamage;
healthBar.value = currentHealth;
nextPoisonDamage = Time.time * PoisonDamageInterval;
poisonEffect = false;
yield return new WaitForSeconds(PoisonDamageInterval);
poisonEffect = true;
}
isPoisoned = false;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612181717im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Status Effect Help 0 Answers
How do I create an undertale player health bar in C#? 1 Answer
How to create a link between HealthScript and HealtBar script ? 1 Answer
Get diable style enemy health 0 Answers
How to place sprite above GameObject? 2 Answers