Okay so I have tried a few different ways to make this temporary invulnerability work, keep failing.
I’m still fairly new to coding, so I don’t fully understand alot of the “leet speak” people usein forums/reddits/etc.
using System.Collections;
using UnityEngine;
namespace SAE
{
public class LightSpeedArmorAbility : Health
{
public bool invincibleAbility;
public bool timeoutActive;
public float CooldownTime; // A cooldown time
public float cooldownUntilNextPress; // The determined value of where the time limit needs to reach.
/// <summary>
/// Start was called in the script I was using inheritance from, so use awake instead.
/// </summary>
public void Awake()
{
cooldownUntilNextPress = 60; // Sets cooldownuntilNextPress to 60 seconds.
invincibleAbility = false; //Set to false
}
/*private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy")
{
hp += float.MaxValue;
invincibleAbility = true; // makes this whole function unusable since invincible is no longer false
}
}*/
private void Update()
{
CooldownTime += Time.deltaTime; // CoolDownTime adds up
if (CooldownTime >= cooldownUntilNextPress) // once greater than 60 seconds you can use input
{
if (Input.GetKeyDown(KeyCode.DownArrow))
{
hp += float.MaxValue;
invincibleAbility = true; // makes this whole function unusable since invincible is no longer false
StartCoroutine(BeginTimeout());
}
}
}
IEnumerator BeginTimeout()
{
timeoutActive = true;
yield return (8f);
CooldownTime = 0;
invincibleAbility = false;
timeoutActive = false;
}
}
}
using UnityEngine;
namespace SAE
{
/// <summary>
/// A Starting point for a Health script, intended to be expanded to meet indivdual games needs, either via
/// direct modification or inheritence.
/// </summary>
public class Health : MonoBehaviour
{
[SerializeField] public int pointValue;
protected ScoreTracker scoreTracker;
[SerializeField] protected float hp;
[SerializeField] protected float maxHp;
[SerializeField] protected bool isAlive = true;
[SerializeField] protected bool isVuln = true;
public bool canHealOnKill = false;
public int healthOnKill;
[SerializeField] protected AudioEffectSO deathAudioEffect;
[SerializeField] protected GameObject deathEffectPrefab;
[SerializeField] protected HealthBar playerHealthBar;
[SerializeField] protected LightSpeedArmorAbility lightSpeed; // Was trying somthing, so ignore this.
public virtual void Start()
{
hp = maxHp;
// Check to see if the gameobject is the player, if so assign the healthbar to be controlled by this script. (also set the max health of the bar!)
if (tag.Contains("Player") && playerHealthBar == null)
{
playerHealthBar = GameObject.FindGameObjectWithTag("HealthBar").GetComponent<HealthBar>();
playerHealthBar.SetMaxHealth((int) maxHp);
Debug.Log("Max Health Set!");
}
lightSpeed.Awake(); // Was trying somthing, so ignore this.
scoreTracker = GameObject.FindGameObjectWithTag("ScoreSystem").GetComponent<ScoreTracker>();
}
public virtual void ReceiveDamage(float dmg)
{
if (!isAlive && !isVuln)
{
return;
}
hp -= dmg;
OnDamage();
OnHealthChange();
if (hp <= 0)
{
isAlive = false;
Death();
}
}
public virtual void AddHP(float amt)
{
hp += amt;
hp = Mathf.Clamp(hp, 0, maxHp);
OnHealthChange();
}
protected virtual void Death()
{
OnHealthChange();
OnDeath();
Destroy(gameObject);
DoDeathEffects();
}
protected virtual void DoDeathEffects()
{
if (deathEffectPrefab != null)
{
Instantiate(deathEffectPrefab, transform.position, deathEffectPrefab.transform.rotation * transform.rotation);
}
if (deathAudioEffect != null)
{
deathAudioEffect.Play2D();
}
}
// this is a good starting point, but eventually you might want to pass more context
protected virtual void OnDamage()
{
}
// To be called whenever the health changes, useful for updating relevant values
protected virtual void OnHealthChange()
{
// Call SetHealth on the UI healthbar and set it to current health
if (playerHealthBar != null)
{
playerHealthBar.SetHealth((int)hp);
Debug.Log("Healthbar Updated");
}
}
public virtual void OnDeath()
{
if (GameObject.FindGameObjectWithTag("Player").GetComponent<Health>().canHealOnKill == true)
GameObject.FindGameObjectWithTag("Player").GetComponent<Health>().AddHP(GameObject.FindGameObjectWithTag("Player").GetComponent<Health>().healthOnKill);
scoreTracker.addScore(pointValue);
scoreTracker.increaseCombo();
}
}
}
Basically what I am trying to do, is when I hit the down arrow; I want # amount of seconds where I can’t take damage, its a certain special ability assigned to the “body” type.
I have managed to make a Decoy drop and a instant 180 for these special abilities of other body types and I am trying to make this last ability standalone in its own script.
(I did comment out the on trigger, but I have tried and failed on using the on trigger function)
So any advice, tips or insight on how to get this invulnerability script to work, would be greatly appreciated.
Thank you in advance for even taking the time to read this.
I’ve read multiple forums, post, etc. But nothing has helped so far.
Its a 2D platformer.