- Home /
How can i wait seconds without a coroutine (C#)?
I'm don't want to use a coroutine, because this is repeating a lot of times and i only need one time.
This is the code: using UnityEngine; using System.Collections;
public class AtaqueMeleeSkeleton: MonoBehaviour {
public GameObject target;
public float tiempoDeAtaque;
public float cooldDown;
public int DañoSkeleton;
Animator animator;
// Use this for initialization
void Start () {
target = GameObject.FindWithTag("Player");
animator = GetComponent<Animator>();
tiempoDeAtaque = 0;
cooldDown = 5.0F;
}
// Update is called once per frame
void Update () {
if (tiempoDeAtaque > 0)
tiempoDeAtaque -= Time.deltaTime;
if (tiempoDeAtaque < 0)
tiempoDeAtaque = 0;
if (tiempoDeAtaque == 0) {
Attack ();
}
}
void Attack (){
float distance = Vector3.Distance (target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot (dir, transform.forward);
//Debug.Log (direction);
//Debug.Log (distance);
if (distance < 2.5) {
if (direction > 0) {
StartCoroutine (restarVida ());
StopCoroutine (restarVida ());
}
}
else {
tiempoDeAtaque = 0;
}
}
IEnumerator restarVida (){
if (Vector3.Distance (target.transform.position, transform.position) < 2.5) {
if (Vector3.Dot ((target.transform.position - transform.position).normalized, transform.forward) > 0) {
animator.SetBool ("Attack", true);
yield return new WaitForSeconds (0.9f);
Debug.Log ("Attack");
EstadoDelPlayer vida = (EstadoDelPlayer)target.GetComponent ("EstadoDelPlayer");
vida.AjustarVidaActual (-DañoSkeleton);
animator.SetBool ("Attack", false);
tiempoDeAtaque = cooldDown;
}
}
}
}
you could simplify your Update:
void Update ()
{
if(attacking == true){ return; }
tiempoDeAtaque -= Time.deltaTime;
if (tiempoDeAtaque < 0)
{
Attack();
}
}
Then in Attack, set the attacking to true
Answer by FWCorey · Apr 23, 2016 at 02:14 AM
Justuse Invoke(method, delay).
http://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
Answer by Happy-Zomby · Apr 10, 2016 at 06:44 PM
hi, 1/ you could use a coroutine but if you don't want to repeat you can do something like
void Update ()
{
if(attack == true)
{
attack = false;
Attack ();
WaitUntilReloaded();
}
}
void WaitUntilReloaded()
{
yield WaitForSeconds(ReloadTime);
attack = true;
}
2/ if it is systematically attacking you could use an invoke repeating http://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html
hope that helps
my bad - that was unity js - I'm in the process of shifting to c#
for the yield in c# - you can see here: http://answers.unity3d.com/questions/350721/c-yield-waitforseconds.html
But i don't want to use coroutines, because is always repeating.
Would be: yield return new WaitForSeconds(ReloadTime);
Yes, this is the correct form, but is a void not a coroutine then WaitForSeconds can not be able.
This cannot work, yield WaitForSeconds(ReloadTime); or yield new WaitForSeconds(ReloadTime);requires a coroutine in Unity.
"How can I wait without a coroutine?" "You can use a coroutine."
Well, thanks.
Answer by Oribow · Apr 10, 2016 at 06:46 PM
You can check in the update function if enough time has passed. Set StartTime to Time.time when you want the timer to start and set timerStart to true.
float startTime = 0;
float waitFor = 4;
bool timerStart = false;
void Update()
{
if (timerStart && Time.time - startTime > waitFor)
{
//Do something
timerStart = false;
}
}
Answer by gvergidis · Dec 27, 2018 at 07:57 PM
Hello.
I read that you do not like to use Coroutine but I am doing something like this. I have a helper script with assisting functions and one of them is :
public void DelayedExecute(float delay, Action onExecute)
{
// Check if delay is valid.
if (delay < 0) return;
StartCoroutine(DelayRoutine(delay, onExecute));
}
private IEnumerator(float delay, Action onExecute)
{
// Wait for given delay
yield return new WaitForSeconds(delay);
onExecute?.invoke();
}
So using this is extreme easy. Just do this :
DelayedExecute(2f, () =>
{
Debug.LogError("I was printed 2 seconds later...");
});
You can further edit this method to parse parameters. Maybe parsing a list of object params.