Reputation: 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class Movement : MonoBehaviour
{
//storage for the object's rigidbody
public Rigidbody2D rb2D;
//the public modifier for speed so i can edit from outside
public float speed;
//reliably take movements each second in an even, reliable manner.
void FixedUpdate()
{
//i move left and right if i use "a" or "d or "left" or "right"
float moveX = Input.GetAxis("Horizontal");
//I move up and down if i use "w" or "s" or "up" or "down"
float moveY = Input.GetAxis("Vertical");
//Vector inputs the move directions into a vector2 so i can move
Vector2 move = new Vector2(moveX, moveY);
rb2D.velocity = move * speed;
}
//following two methods are for the respective spikey builds
public void slow()
{
print("Shit, spike's got me!");
speed = .3f;
StartCoroutine(PauseTheSlowdown(2.1f));
speed = 1;
}
public void Bigslow()
{
print("HOLY CRAP THAT HURT");
speed = 0f;
StartCoroutine(PauseTheSlowdown(3.7f));
speed = 1;
}
IEnumerator PauseTheSlowdown(float seconds)
{
print("Pause this crap");
yield return new WaitForSecondsRealtime(seconds);
}
}
I've encountered a bit of an issue here where my code seems to be perfectly fine. It runs, and then when an external script goes to pull from either of the spike methods, it behaves like the coroutine isn't even there and goes right from slow-down back to full speed. There are no errors or issues other than the coroutine simply does not cause a wait to happen. Am I missing something obvious here?
Upvotes: 0
Views: 973
Reputation: 125455
You can't put the code that has to pause in a normal function. You have to put it inside the coroutine function.
For example, in your Bigslow
function, Speed
variable will be set to 0
. The PauseTheSlowdown
coroutine will be started. Unity will wait one frame only in the PauseTheSlowdown
function then return to the Bigslow
where speed is set to 1
. It will not wait because you are starting it from a void
function.
You have two options:
1.Make your slow
and Bigslow
functions to be IEnumerator
instead void
then yield when StartCoroutine
is called.
public IEnumerator slow()
{
print("Shit, spike's got me!");
speed = .3f;
yield return StartCoroutine(PauseTheSlowdown(2.1f));
speed = 1;
}
public IEnumerator Bigslow()
{
print("HOLY CRAP THAT HURT");
speed = 0f;
yield return StartCoroutine(PauseTheSlowdown(3.7f));
speed = 1;
}
IEnumerator PauseTheSlowdown(float seconds)
{
print("Pause this crap");
yield return new WaitForSecondsRealtime(seconds);
}
Notice the yield return
before calling StartCoroutine
.
2.Move the code you want to execute and wait for some time to the PauseTheSlowdown
coroutine function:
public void slow()
{
print("Shit, spike's got me!");
StartCoroutine(PauseTheSlowdown(2.1f, .3f, 1));
}
public void Bigslow()
{
print("HOLY CRAP THAT HURT");
StartCoroutine(PauseTheSlowdown(3.7f, 0f, 1));
}
IEnumerator PauseTheSlowdown(float seconds, float before, float after)
{
print("Pause this crap");
speed = before;
yield return new WaitForSecondsRealtime(seconds);
speed = after;
}
Upvotes: 2