- Home /
Reversing countdown timer with an OnTriggerEnter event so the timer adds time via Time.deltaTime until it reaches max assigned time.
Hi, I have the following timer script that counts down via an image the slides. I would like the timer to count back up toward my publicly assigned maxTime so long as the player is within a game object's box collider. I've tested the trigger even between the player and the object collider and it is working, but I am having difficulty figuring out the right code to reverse the countdown timer to have it add time. I have two scripts, one for the timer and the other to be attached to the player. The one attached to the player is where I am stuck. Could use some advice.
Timer Script (This works so long as it is counting down. Cannot figure out how to reverse the countdown.)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class StatusBarScripts : MonoBehaviour {
public Image spiritualStrengthBar;
public float maxTime = 60f;
public GameObject spiritualPool;
private float timeLeft;
IEnumerator coroutine;
// Start is called before the first frame update
void Start()
{
StartCoroutine(DelayTimer());
spiritualStrengthBar = GetComponent<Image>();
timeLeft = maxTime;
}
// Update is called once per frame
void Update()
{
if (timeLeft > 0)
{
timeLeft -= Time.smoothDeltaTime;
spiritualStrengthBar.fillAmount = timeLeft / maxTime;
}
else
{
Time.timeScale = 0;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player" && other.gameObject.tag == "SpiritPool")
{
print("Event Triggered");
}
}
IEnumerator DelayTimer()
{
yield return new WaitForSeconds(15);
}
}
Script attached to player (I've tried many different ways to make this work, but none of them have done the trick.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class SpiritPool : MonoBehaviour {
public Image spiritualStrengthBar;
public float maxTime = 60f;
public GameObject spiritPool;
private float timeLeft;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
print("Event Triggered");
timeLeft -= Time.smoothDeltaTime;
spiritualStrengthBar.fillAmount = timeLeft / maxTime;
}
}
}
Answer by bpaynom · Mar 06, 2019 at 08:56 AM
First of all, DelayTimer does nothing for you. Check the CoroutinesManual.
On the other hand, you are modifying the fillamount of the image with two "random" values. Instead, I'd suggest you to update the timeLeft of your StatusBarScripts from a reference in the player script.
// In your StatusBarScripts
public void IncrementTime ( float amount )
{
timeLeft += amount;
}
And then
//In your Player Script, in your Trigger function
myReferenceToStatusBarScripts.IncrementTime( -Time.smoothDeltaTime );
Your answer
Follow this Question
Related Questions
animation wont reverse 0 Answers
What the heck is a 'function(System.Array): void' Type? 2 Answers
C# Movement reversed with GridMove script? 0 Answers
Pairing and GUITexture with a Timer? 1 Answer
How to call time function once time? 0 Answers