- Home /
How to add return functionality to the script if input is not detected
Can someone help me out here, please;
Here below, is my code. So far, its working though I'm trying to add another functionality to it.
The script allows my character to attack when isAttacking is true and wait until cooldownDuration is complete but I also would like it to return if the Attack wasn't done (even when canAttack is true) : that's why I have the returnTime variable {
public bool canAttack;
public bool isAttacking;
public float maxTime;
public float trueTime;
public float returnTime;
public float cooldownDuration;
PlayerInput input;
PlayerCharacter character;
private void Awake()
{
input = GetComponent<PlayerInput>();
character = GetComponent<PlayerInput>();
trueTime = 0;
}
private void FixedUpdate()
{
StartCoroutine(GroundAttackCheck());
}
IEnumerator GroundAttackCheck()
{
canAttack = false;
isAttacking = false;
if (!(character.isGrounded && input.interactHeld))
{
canAttack = false;
}
if (character.isGrounded && input.interactHeld)
{
trueTime += Time.deltaTime;
if (trueTime < maxTime)
{
canAttack = true;
}
if (canAttack && input.slicingHeld)
{
isAttacking = true;
trueTime = maxTime;
yield return new WaitForSeconds(cooldownDuration);
trueTime = 0;
}
if (trueTime >= maxTime)
{
trueTime = maxTime;
}
}
}
}
I think you're better off moving most of that out of the coroutine entirely.
Sorry, I'm not entirely sure of what you mean?
What you mean exactly with "would like it to return" ??
When holding the button, "InteractHeld", the "canAttack" becomes true for a set time(currently its 2 sec and this will allow the ). If I press the Attack button (input.isslicingHeld), the "isAttacking" boolean becomes true. Immediately after this, everything resets after the cooldownDuration
What I'll like to add is, if the "canAttack" is true for that 2 second duration and I didn't press the Attack button, I want it to wait for another duration (returnTime) before I'm able to cause the canAttack to be true
As summary, I just want to wait for just few seconds to be able to restart the process and that's going back to being able to attack again if canAttack is initiated but there's no Attack input
This's similar to cases where a player is meant to supercharge an attack for a specified duration and release such attack but what if the player successfully supercharges the weapon and didn't press the final button to release the attack. That's my problem
Answer by tormentoarmagedoom · May 20, 2019 at 04:15 PM
Then you can use a Invoke with 2 seconds.
IF detect de atack, execute CAncelInvoike.
In no detection, the invoke will be executed after that 2 seconds.
Bye!
Your answer

Follow this Question
Related Questions
How can i use static function in ScriptableObject? 3 Answers
collider affect collider problem 0 Answers
Zombie script 1 Answer
Take damage after seconds 3 Answers
Attack,Health and enemy health. 1 Answer