- Home /
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
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 :)
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
np any time, just select this for the right answer so that it will be resolved and ppl won't enter anymore :)