0

I would like to ask how to trigger an action or function after some time. In my case, I have a function class timer which shows in below as FuntiontTimer.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FuncTime
{
private Action action;
private float timer;
private bool isDestroyed;


public FuncTime(Action action, float timer)
{
    this.action = action;
    this.timer = timer;
    isDestroyed = false;
}

public void Update()
{
    if (!isDestroyed) { 
        timer -= Time.deltaTime;
        if (timer < 0)
        {
            action();
            DestroySelf();
        }
    }
}

private void DestroySelf()
{
    isDestroyed = true;
}
}

The code works fine while I test with testing.cs below with MonoBehaviour

public class Testing : MonoBehaviour
{
private FuncTime funcTimer;

private void Start()
{
    funcTimer = new FuncTime(Action, 3f);
}

private void Update()
{
    funcTimer.Update();
}

private void Action()
{
    Debug.Log("Testing!");
}
}

But then, when I put to code below, is not working. I would to have cameraShake active after 30f from time where I start.

public class CameraShaker : MonoBehaviour
{
public float power = 0.7f;
public float duration = 1.0f;
public Transform camera;
public float slowDownNo;
public bool shaking;

Vector3 startPosition;
float initialDuration;

void Start()
{
    camera = Camera.main.transform;
    startPosition = camera.localPosition;
    initialDuration = duration;
}

// Update is called once per frame
void Update()
{
    if (shaking)
    {
        if(duration > 0)
        {
            camera.localPosition = startPosition + Random.insideUnitSphere * power;
            duration -= Time.deltaTime * slowDownNo;
        } else
        {
            shaking = false; //time can be placed here
            duration = initialDuration;
            camera.localPosition = startPosition;
        }
    }
}
}
Remy
  • 4,843
  • 5
  • 30
  • 60
Diah Ayu
  • 3
  • 1
  • 2
  • Hi, I see you are using the [tag:unityscript] tag, please note that this is (as explained in the tag info) a deprecated scripting language made by Unity, and not an encompassing term for all scripts in Unity. Your question is about C# (and I have removed the tag for you). Please only use this tag if you're working on legacy code using the actual language UnityScript. This keeps the question and tags in line with each other :) – Remy Aug 04 '20 at 06:10

1 Answers1

1

One solution is scheduling work to be done in the future by using a Coroutine.

Something like this:

IEnumerator myWaitCoroutine()
{
    yield return new WaitForSeconds(1f);// Wait for one second

    // All your Post-Delay Logic goes here:
    // Run functions
    // Set your Values
    // Or whatever else
}
StartCoroutine(myWaitCoroutine());

The everything in the curly braces {} is just being declared and doesn't actually run until you call StartCoroutine(myWaitCoroutine);

So in your case you could start the shake then create a stopShakeAfterDelayCoroutine(). Something like:

StartShake();

IEnumerator stopShakeCoroutine()
{
    yield return new WaitForSeconds(shakeDuration);// Wait a bit
    StopShake();
}
StartCoroutine(stopShakeCoroutine());

Then of course you can still try to stop the shake prematurely in your Update() if you need to.