|
Post by kyomori on Oct 23, 2017 11:27:23 GMT
Hi guys, can someone help me? I want to make my keyboard button lock or waiting for cooldown while I'm using skills, but when I pressed it, it only show my skill slot cooldown, I still can spam keyboard button, how can I fix this? Here is for videocode using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class SkillsCooldown : MonoBehaviour {
public List<Skill> skills;
void Start() { foreach (Skill s in skills) { s.currentCooldown = s.cooldown; } }
void FixedUpdate() { if (Input.GetKeyDown (KeyCode.Alpha1)) { if (skills [0].currentCooldown >= skills [0].cooldown) { skills [0].currentCooldown = 0; } } else if (Input.GetKeyDown (KeyCode.Alpha2)) { if (skills [1].currentCooldown >= skills [1].cooldown) { skills [1].currentCooldown = 0; } } else if (Input.GetKeyDown (KeyCode.Alpha3)) { if (skills [2].currentCooldown >= skills [2].cooldown) { skills [2].currentCooldown = 0; } } }
void Update() { foreach (Skill s in skills) { if (s.currentCooldown < s.cooldown) { s.currentCooldown += Time.deltaTime; s.skillIcon.fillAmount = s.currentCooldown / s.cooldown; } } } }
[System.Serializable] public class Skill { public float cooldown; public Image skillIcon; [HideInInspector] public float currentCooldown; }
|
|
|
Post by sickscore on Oct 23, 2017 19:56:53 GMT
Just trigger your attacks after each „skills [...].currentCooldown = 0;“ line manually. So it will only fire, once the cooldown is ready again. You ‚could‘ prevent the button event, but that‘s really bad practice and will result in more issues.
Cheers, sickscore
|
|
|
Post by kyomori on Oct 24, 2017 3:08:44 GMT
Just trigger your attacks after each „skills [...].currentCooldown = 0;“ line manually. So it will only fire, once the cooldown is ready again. You ‚could‘ prevent the button event, but that‘s really bad practice and will result in more issues. Cheers, sickscore Thank you for reply, I triggered attack animation from Invector's animator and use attack(); after skills [...].currentCooldown = 0; but not working, here is the code void FixedUpdate() { if (Input.GetKeyDown (KeyCode.Alpha1)) { if (skills [0].currentCooldown >= skills [0].cooldown) { skills [0].currentCooldown = 0; attack (); } } else if (Input.GetKeyDown (KeyCode.Alpha2)) { if (skills [1].currentCooldown >= skills [1].cooldown) { skills [1].currentCooldown = 0; attack (); } } else if (Input.GetKeyDown (KeyCode.Alpha3)) { if (skills [2].currentCooldown >= skills [2].cooldown) { skills [2].currentCooldown = 0; attack (); } } }
void Update() { foreach (Skill s in skills) { if (s.currentCooldown < s.cooldown) { s.currentCooldown += Time.deltaTime; s.skillIcon.fillAmount = s.currentCooldown / s.cooldown; } } } void attack () { if (Input.GetKeyDown (KeyCode.Alpha1)) { anim.SetTrigger ("StrongAttackA"); } else if (Input.GetKeyDown (KeyCode.Alpha2)) { anim.SetTrigger ("Cast"); } else if (Input.GetKeyDown (KeyCode.Alpha3)) { anim.SetTrigger ("Skill Swords"); } } }
|
|
|
Post by sickscore on Oct 24, 2017 4:10:52 GMT
That’s basically the same logic than before. You call the same method again and again. Just delete the whole attack method and write „anim.SetTrigger ("FancyAttack");“ right after the currentCooldown = 0, instead of „attack()“.
Btw, don‘t use FixedUpdate for input handling. Move the code into the Update() method. FixedUpdate does not necessarily fire every frame, so input WILL be lost sometimes (especially on slower machines).
|
|
|
Post by kyomori on Oct 24, 2017 5:12:47 GMT
Hi sickscore, thank you for your help, now it fixed!
|
|