- Home /
Question by
RVDL_IT · Mar 13, 2018 at 09:45 PM ·
c#if-statementsattackgetkeydowngetkey
How to get holding key to prioritize
I'm making an attack script with a normal attack (tap key), and a charged attack (hold key). When I hold the key to chrage, it charges and does a normal attack. Is there any way that I can fix this? Any help is appreciated.
Comment
You will need to give a bit more information. At a bare $$anonymous$$imum, how / where are you checking button presses? Can you post your code?
This is the important part of it.
//Normal Sword Swings
if(SwingTime > 0){
Sword.transform.Rotate(Vector3.back * Time.deltaTime * SlingSpeed);
SwingTime -= Time.deltaTime;
}
if(SwingTime < 0){
SwingTime = 0;
SwordReset = SRTime;
}
if(SwordReset > 0) {
SwordReset -= Time.deltaTime;
}
if(SwordReset < 0) {
SwordReset =0;
Sword.transform.rotation = new Quaternion(0, 0, 0, 0);
RSword.GetComponent<SpriteRenderer>().enabled = false;
}
//Charged Sword Swings
if(ChargingTime > 0) {
ChargingTime = 0;
}
//Charges Attacks
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow)){
ChargingTime -= Time.deltaTime;
}
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.UpArrow) || ChargingTime == 0) {
Sword.transform.rotation = Quaternion.Euler(0, 0, 0);
ChargingTime = ChargeTime;
SwingTime = ChaSwingTime:
}
//Normal Attacks
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow) && SwordReset == 0){
Sword.transform.rotation = Quaternion.Euler(0, 0, 45);
SwingTime = STLength;
RSword.GetComponent<SpriteRenderer>().enabled = true;
RSword.GetComponent<BoxCollider>().enabled = true;
}
Answer by ShadyProductions · Mar 13, 2018 at 10:19 PM
Here is a way you can achieve the effect you want:
float holdStartTime = 0f;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
holdStartTime = Time.time;
}
if (Input.GetMouseButtonUp(0))
{
float delta = Time.time - holdStartTime;
if (delta > 0.1)
{
//held longer than .1 second click
// do special attack
}
else
{
// do normal attack
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612154534im_/https://answers.unity.com/themes/thub/images/avi.jpg)