- Home /
How can I synchronize the cooldown on two separate scripts?
I've got a script setup to turn particle effects on for an ability, play for specified ability time and then go on cool down. Now one of my abilities is triggering a particle effect on my player as well as on another object.
I have it setup so that when the player is in the object's collider the specified key will turn the particles on. This all works fine except when I used the ability outside of the collider. In this case, the player's particles will turn on which is what I want, but then if I move into the collider and use the ability again it will trigger only that objects particles.
Problem is essentially that both sets of particles need to be on cooldown at the same time, regardless of if my player is in the collider to trigger the objects set of particles.
I'd actually like to find a way that I could just put my "fire2" input itself on cooldown for X Seconds globally but haven't figured that out either.
This is the script on my non player object:
#pragma strict
var MobisPlaying = false;
var MobcoolDown = 1.5;
function OnTriggerStay (other : Collider) {
if (!MobisPlaying && Input.GetButtonUp("Fire2")) {
PlayMPS();
}
}
function PlayMPS() {
MobisPlaying = true;
particleSystem.Play();
yield WaitForSeconds(0.5);
particleSystem.Stop();
yield WaitForSeconds(MobcoolDown);
MobisPlaying = false;
}
This is what is on my player:
#pragma strict
var isPlaying = false;
var coolDown = 1.5;
function Update() {
if (!isPlaying && Input.GetButtonUp("Fire2")) {
PlayPS();
}
}
function PlayPS() {
isPlaying = true;
particleSystem.Play();
yield WaitForSeconds(0.5);
particleSystem.Stop();
yield WaitForSeconds(coolDown);
isPlaying = false;
}
Answer by FreeTimeDev · Oct 18, 2014 at 12:51 PM
If you wanted to make the input have a cooldown globally because of it being used so much you'd probably want to make a static boolean associated with it on the player script.
Make a static boolean (CanFire2) When the input is pressed, on the player script, check to see if they CanFire2 if yes, set it to false and make a timer if no, do nothing on all other scripts you will easily be able to access CanFire2 because it's static. You don't have to check for inputs, you'd just check for the state of CanFire2.
I have a hunch there's a better way to do what you want, but I learned to do things (sometimes the wrong way!) a little out of the ordinary. And if it works...it works.
Answer by steelesound · Oct 19, 2014 at 06:04 AM
I understand the concept of what you're saying and I think it's going to accomplish what I'm looking for. I'm not quite sure how to lay that out in code though, would you mind elaborating on it a bit?
Sorry, I am indeed very new and most of the scripts I have put together are based off of what I can find here. I'm not so good at writing them from scratch.
For the player script, I have something like this and I don't quite think I'm on the right track:
pragma strict
var canFire2 : boolean = false;
function Update() {
if (Input.GetButtonDown("Fire2")) {
PlayCanFire2();
}
}
function PlayCanFire2() {
canFire2 = true;
yield WaitForSeconds(12.0);
canFire2 = false;
}
Your answer
Follow this Question
Related Questions
Interacting with a button by colliding with it 1 Answer
Can't click gameobject when over another trigger? 1 Answer
detect Collision/Trigger seperately for more than one collider in one gameObject 0 Answers
Input keycode string not working 2 Answers
Trigger collider triggers even though colliders are not touching 2 Answers