- Home /
Question by
jonathanbooker2911 · Dec 01, 2018 at 07:25 AM ·
scripting problemscript.scripting beginnerspellspells
loop vs if spell casting script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class InstantSpells : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public int charges = 2;
public int maxcharges = 3;
public float chargecoolDown;
public float chargecoolDownTimer;
public Camera fpsCam;
void Start()
{
charges = 2;
maxcharges = 3;
}
void Update()
{
ChargeSystem();
ChargeCoolDown();
}
void ChargeSystem()
{
if (charges < 3 && chargecoolDownTimer == 0)
{
charges++;
}
if (charges > 3 && chargecoolDownTimer == 0)
{
charges--;
}
if (charges == 0)
{
}
if (Input.GetKeyDown("h") && charges > 0)
{
charges--;
chargecoolDownTimer = chargecoolDown;
Shoot();
}
}
void ChargeCoolDown()
{
if (chargecoolDownTimer > 0)
{
chargecoolDownTimer -= Time.deltaTime;
}
if (chargecoolDownTimer < 0)
{
chargecoolDownTimer = 0;
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
EnemyHealth enemyHealth = hit.transform.GetComponent<EnemyHealth>();
if (enemyHealth != null)
{
enemyHealth.TakeDamage(damage);
}
}
}
}
I want this script to run 3 different cooldowns for each charge of the spell but should I use loops or if statements. What are the pros and cons?
Comment
Answer by hexagonius · Dec 01, 2018 at 12:02 PM
It's not a question of pro or con in loop or if statement. The choice will come when you know what you want to do exactly, figure that out first