Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by ReZo · May 04, 2014 at 07:48 AM · variableforcestophow to

How to "Lock" a variable for X seconds

I have searched Google and here for 2 hours and have come up with nothing relating to my question

TL;DR - an if statement inside the "BossController" script, inside the Update function, sets "colliderA" (box collider attached to an Empty game object, parented to the wrist joint on the bosses hand) to enabled = true; if the boss is attacking(Cue Animation)

When the colliderA enters the players "colliderB" (the Collider attached to the player), the "DamagePlayer" script (which is attached to same object as colliderA) sets colliderA to enabled = false;

The problem I am having is that since the if statement that sets colliderA to true is inside my Update() function, it sets colliderA back to true on the next frame or two.

Which then results in the Player getting hit twice.

How do I "Lock" the collider to false for x seconds (Even if the update is trying to turn it to true each frame)

Alright, So I have two scripts. "BossController" and "DamagePlayer"

DamagePlayer is attached to Two different empty gameObjects, both with box colliders on them. "HandTrigger_R" and "HandTrigger_L" Both are attached to their respective wrist joints on the bosses rig.

The HandTrigger colliders are set to false until the bosses "attackTimer" (Which is nothing more than a float variable set to count up by Time.deltatime if the boss is attacking) reaches the "attackWaitTime" (again float, set to 2.0f, and changes based on the bosses health)

When the bosses attackWaitTime is reached, the boss then plays his attack animation, and slams down his hand connecting with the players collider, thus triggering the OnTriggerEnter function inside of the DamagePlayer script.

This function reduces the players health by 1 and then sets the HandTrigger colliders to enabled = false if they connect with the players collider.

Since the if statement that enables the HandTrigger colliders is in the update function, it is being called again the very next frame or two, and sets the HandTrigger colliders to enabled = true

Which results in the player getting hit twice.

How would I go about "locking" the HandTriggers collider enabled = false; for a few seconds to give the attack animation enough time to pull away the hand and the collider from the player. Even if the Update function is trying to enable it each frame.

inside the BossController script:

 void Update () 
     {
         if(bossAwake)
         {
             //Irrelevant code
 
             if(inBattle)
             {
                 //Irrelevant Code
 
                 if(!attacking)
                 {
                     //Irrelevant Code
                 }
 
                 else
                 {
                     
                     attackTimer += Time.deltaTime;
                     anim.SetBool ("attackReady", true);
 
                     if(attackTimer >= attackWaitTime)
                     {
                         attacking = false;
                         anim.SetTrigger ("bossAttack");
 
                         attackTimer = 0.0f;
                         anim.SetBool ("attackReady", false);    
                         
                         handTrigger_L.collider.enabled = true;
                         handTrigger_R.collider.enabled = true;
                     }
                 } //Irrelevant Code Until End of Update function




Inside DamagePlayer script:

 void OnTriggerEnter (Collider other)
     {
         if(other.tag == "Player")
         {
             characterHealth.health--;
             bossController.handTrigger_L.collider.enabled = false;
             bossController.handTrigger_R.collider.enabled = false;
 
             
         }
     }


Sorry for such a long post

Thank you in advance for any information.

I have been finishing coding this game, along with 3 other games and a 3D modeling Final, and a virtual environment final, and a programming Final for College all week.

I have had about 18 hours of sleep in the past 7 days, and its all due Tuesday 5/6/2014.

I am hoping due to my sleep deprivation, that simply my logic is off.

Thank you for reading any of this, and for your time

-ReZo

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by bloodyatheist · May 04, 2014 at 09:11 AM

you can just add a bool variable: bool locked=false;

and in that if statement in the Update() just add this

 if(...&&!locked)
 {
 ...
 }

(or you can an if statement just around the the trigger state change to make sure the object is not locked)

when you're locking you can do this:

 locked=true;
 //add here other things you want to do when locking
 StartCoroutine(WaitAndUnlock(xSeconds));

and the coroutine will look like this:

 IEnumerator WaitAndUnlock(float waitTime) {
     yield return new WaitForSeconds(waitTime);
     locked=false;
     //add here other things you want to do when unlocking
 }

i hope i helped :)

Comment
Add comment · Show 3 · 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
avatar image ReZo · May 04, 2014 at 09:56 AM 0
Share

Thanks for the response!

I had just finished fixing this right before I checked this post!

I got tired of messing with a box collider that $$anonymous$$oved/Rotated with the bosses hand, ended up making a collider that covered the exact area I wanted the boss to hit (one for right, one for left)

I ended up simply creating an event in the animation(one for right and one for left), and called a function during the animation that turned it on, then added the damageplayer script to the colliders and just turned them off right after characterHealth.health--;

Gonna save this in my bookmarks for future reference, Thanks :D

avatar image bloodyatheist · May 04, 2014 at 10:01 AM 0
Share

np any time, just select this for the right answer so that it will be resolved and ppl won't enter anymore :)

avatar image ReZo · May 04, 2014 at 10:06 AM 0
Share

Done and Done :)

Thanks again!

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

21 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

Related Questions

How should I implement jumping into this script? 0 Answers

how to make the character stops temporarily in the start 1 Answer

How come my boost comes to an abrupt stop? 1 Answer

How do I spread a movement over several frames instead of one? 1 Answer

stop adding force 2 Answers


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