Im trying to make the script stop for some seconds (so it can play the partical at a part where the animation is playing). ive looked pretty much everywhere but im clueless. can somone explain?
// some stuff
[SerializeField] ParticleSystem PS;
public Animation anim;
public GameObject Player;
public int Cooldown;
private float CDnumber;
private bool Activated = false;
private bool CoolDownEnabled = false;
public float ParticleWaitTime;
// the main code
void OnTriggerEnter2D(Collider2D collider2D)
{
if (collider2D.gameObject.name == Player.name)
{
if (Activated == false && CoolDownEnabled == false)
{
anim.Play();
// Where im trying to put the wait for 3 frames
}
}
}
Hey! You can use coroutines!
// coroutine function
IEnumerator yourCoroutine(){
// some action
// then waiting 1 secound
yield return new WaitForSeconds(1f);
// some actions
}
// calling coroutine e.g:
void Start()
{
// some actions
StartCoroutine(yourCoroutine());
}
It’s a bit hard to really know what you mean by
make the script stop
But he easiest way would be to put a frame counter in the script and use the Update()
method to do the playing/stopping , like this maybe
bool isPlayingAnim;
int animDurationInFrames;
int currentFrameCounter;
// the main code
void OnTriggerEnter2D(Collider2D collider2D)
{
if (collider2D.gameObject.name == Player.name)
{
if (Activated == false && CoolDownEnabled == false)
{
anim.Play();
isPlayingAnim = true;
currentFrameCounter = 0;
}
}
}
private void Update()
{
// will exit if the anim is playing
if(isPlayingAnim)
{
currentFrameCounter++;
isPlayingAnim = currentFrameCounter < animDurationInFrames;
return;
}
// todo : add the code you want to execute after the wait
}
You could disable the script.
When you want it to stop do the following code:
referenceToScript.enabled = false;
Invoke("EnableScriptAgain", floatForHowLongToDisable);
And have the following function on the object running that code:
void EnableScriptAgain(){
referenceToScript.enabled = true;
}