- Home /
Integer gets too many additions
I wrote a quick combo script but the if statement gives the combo a value of about 14 as opposed to 1. I think that the problem is that it checks every frame. Is there a way that it can only check that for 1 frame?
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ComboSystem : MonoBehaviour {
//Hit Types
public bool NormalAttack;
public bool ChargedAttack;
public bool DashAttack;
//Combo Timer Variables
public float ComboTime;
public float ComboTimer;
//Combo Variables
public int Combo;
void Update() {
//Hit references
NormalAttack = this.gameObject.GetComponent<AttackControls>().NormalAttack;
ChargedAttack = this.gameObject.GetComponent<AttackControls>().ChargedAttack;
DashAttack = this.gameObject.GetComponent<AttackControls>().DashAttack;
//Combo Timers
if(NormalAttack == true || ChargedAttack == true || DashAttack == true) {
ComboTime = ComboTimer;
Combo += 1;
}
if(ComboTime > 0){
ComboTime -= Time.deltaTime;
}
if(ComboTime < 0){
ComboTime = 0;
}
if(ComboTime == 0){
Combo = 0;
}
}
}
Answer by Menyus777 · Mar 20, 2018 at 10:19 PM
Dont really understand what u wants, but Update is being called once in every frame, if you want something to run in every 14th frame then you should use a Coroutine also what u wrote is also should be a Coroutine. to start a Coroutine in unity use StartCoroutine(RoutineName);
private IEnumerator ICombat()
{
//do something
yield return new WaitUntil(this_is_a_predicate_which_waits_until_14th_frame);
}
Answer by Priyanka-Rajwanshi · Mar 23, 2018 at 01:06 PM
@RobertVanDerLeeuw
It seems that your variables NormalAttack or ChargedAttack or DashAttack remain true for more than 1 update frame, that is why you get a value of 14 instead of 1.
Try incrementing the "Combo" value at the instance where you are updating the boolean variables to true (If the latter is not in Update)
Your answer
Follow this Question
Related Questions
Scavenger Hunt List Bools Question 1 Answer
c# Ignoring conditional statement? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Some Boolean "Action" Trouble 2 Answers