Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PreciousBliss · May 20, 2019 at 02:34 PM · attackreturn

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;
         }
     }
 }

}

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · May 20, 2019 at 02:37 PM 0
Share

I think you're better off moving most of that out of the coroutine entirely.

avatar image PreciousBliss TreyH · May 20, 2019 at 03:33 PM 0
Share

Sorry, I'm not entirely sure of what you mean?

avatar image tormentoarmagedoom · May 20, 2019 at 03:40 PM 0
Share

What you mean exactly with "would like it to return" ??

avatar image PreciousBliss tormentoarmagedoom · May 20, 2019 at 04:00 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges