Hi guys,
trying to add more functionality to my Timescale option, in which the player can press a button; timescale is reduced for 5 seconds, then returns to normal before being recieving a timescale option again for another 5 seconds (to prevent consistent usage over and over)
heres my code… but i’m having no luck!
WaitForSeconds is affected by TimeScale, and you need to start a coroutine explicitely in c#. You probably want something like this:
private float timeSlowFactor = 0.2f;
private float timeSlowDuration = 5f;
public void StopTime()
{
if (Input.GetButton("timescalebutton"))
Time.timeScale = timeSlowFactor;
StartCoroutine(ResumeTime());
}
}
public IEnumerator ResumeTime() {
yield return new WaitForSeconds(timeSlowDuration * (1f / timeSlowFactor));
Time.timeScale = 1f;
}
Cheers, but i’m getting error; "The body of my method cannot be an iterator block because void is not an iterator or interface type.
Should work now.
I often edit after I post, bad habit but something about writing code in the form box makes me gloss over errors.
you need to use IEnumerator.
Cheers for the quick responses guys,
I’ve tried that and it doesn’t like IEnumerator for some reason; (error displayed below)
public IEnumerator ResumeTime()
{
yield return new WaitForSeconds(timeSlowDuration * (1f / TimeSlowFactor));
Time.timeScale = 1f;
}
Expected class, delegate , enum, interface or structure error… :\
In order to use yield the function it is in must return IEnumarator, and you must match your curly braces. ie.
public IEnumerator MyStallerFunction ()
{
// stall for 5 seconds
yield return new WaitForSeconds(5.0f);
}
as for your main question;
if you want to catch the key presses, you need to put it in Unity’s Update () function, below code should get you going;
// Update is called once per frame
void Update ()
{
// catch if player has pressed 7
if (Input.GetKey (KeyCode.Alpha7)) {
//if (Input.GetButton ("timescalebutton")) {
StartCoroutine (StopTime ());
}
}
public IEnumerator StopTime ()
{
Debug.Log ("Slowing time scale");
Time.timeScale = 0.2f;
// 5 seconds, since the time scale is one fifth of normal now.
yield return new WaitForSeconds(1.0f);
Time.timeScale = 1;
Debug.Log ("Time scale is normal again.");
}
put using System.Collections at the top of the file
also ensure you are closing braces
Ok this works as it should anyone know HOW to make this a repeating function? i.e. if this is pressed again; it will work again, as at the moment it only works once.
Bump.
Sorry for the bump but i just changed my previous post!
after i got it working to an extent.
It’s not actually working once. since your call to ResumeStandardTime function is in Update (); it is run in every frame. I guess this is not what you intend to do. So, you need to put that call in somewhere controlled. For instance, put curly braces of your if statement,
if (Input.GetButton("Stop Time Button"))
{
Time.timeScale = TimeSlowFactor;
StartCoroutine(ResumeStandardTime());
}
this way it should work as you wanted.
Thats exactly what I wanted cheers!
how would i add a timer, between it finishing so it cannot be used again for 30 seconds for example?
Create a variable that is set when going into the StopTime() function and compare that the next time the button is pressed or StopTime() is called again.
pseudocode:
if (currenttime-lastStopTime > 30 seconds)
StopTime();
else
print “Must recharge your time bending ability”;
sorry for the pseudocode, I have not memorized the object heirarchy of the Time class yet.