- Home /
The question is answered, right answer was accepted
Enemies Jump at Random with Coroutines 2d (C#) Error.
Hello, i was testing the controllers for my enemies in a 2D plataformer game, making them go left an right, like a MarioBros game, and i woud like them to jump at random times. The problem seams to be that the collider go in and out the EstaTocandoElSuelo (OnGrounded) even when there ir no jump and it´s only moving betwen the X, and the coroutine make the sprite look like a hummingbird...
[SerializeField] int velocidadDeMovimiento = 10;
[SerializeField] int alturaDeSaltoGatos = 5;
[SerializeField] float minSegundosEntreSaltos = 2f;
[SerializeField] float maxSegundosEntreSaltos = 5f;
[SerializeField] bool EstaTocandoElSuelo = false;
Rigidbody2D gatoRigidbody;
Coroutine gatoSaltandoCoroutine;
void Update()
{
MovimientoGato();
}
public void MovimientoGato()
{
gatoRigidbody.velocity = new Vector2(velocidadDeMovimiento, transform.position.y);
gatoSaltandoCoroutine = StartCoroutine(SaltoDelGato());
if (!EstaTocandoElSuelo)
{
StopCoroutine(gatoSaltandoCoroutine);
}
}
IEnumerator SaltoDelGato()
{
yield return new WaitForSeconds(Random.Range(minSegundosEntreSaltos,maxSegundosEntreSaltos));
if (EstaTocandoElSuelo)
{
gatoRigidbody.AddForce(new Vector2(0, alturaDeSaltoGatos), ForceMode2D.Impulse); ;
}
}
void OnCollisionEnter2D(Collision2D volviendoAlSuelo)
{
if (!EstaTocandoElSuelo & volviendoAlSuelo.gameObject.CompareTag("EnTierra"))
{
EstaTocandoElSuelo = true;
}
}
private void OnCollisionExit2D(Collision2D saliendoDelSuelo)
{
if (EstaTocandoElSuelo & saliendoDelSuelo.gameObject.CompareTag("EnTierra"))
{
EstaTocandoElSuelo = false;
}
}
I would appreciate if the variables would be named in English. It is hard to understand the code, if I don't know what it means.
Hi!
I've commented your script as understood, so if the logic really looks like this:
void Update()
{
// Calls function EACH frame
$$anonymous$$ovimientoGato();
}
public void $$anonymous$$ovimientoGato()
{
// Adjust velocity EACH frame
gatoRigidbody.velocity = new Vector2(velocidadDe$$anonymous$$ovimiento, transform.position.y);
// Starts coroutine EACH frame
gatoSaltandoCoroutine = StartCoroutine(SaltoDelGato());
// If not grounded stops coroutine
if (!EstaTocandoElSuelo)
{
StopCoroutine(gatoSaltandoCoroutine);
}
}
IEnumerator SaltoDelGato()
{
// Wait for random seconds between $$anonymous$$ and max declared
yield return new WaitForSeconds(Random.Range($$anonymous$$SegundosEntreSaltos, maxSegundosEntreSaltos));
// If grounded then addforce up
if (EstaTocandoElSuelo)
{
gatoRigidbody.AddForce(new Vector2(0, alturaDeSaltoGatos), Force$$anonymous$$ode2D.Impulse); ;
}
}
Then the issue that you are calling multiple coroutines. Keep in $$anonymous$$d that if you have 300 frames per second then you are calling 300 coroutines which gonna execute except for one last assigned that you stop when not grounded.
Try this one:
bool is_CanJump = true;
void Update()
{
// Calls function EACH frame
$$anonymous$$ovimientoGato();
}
public void $$anonymous$$ovimientoGato()
{
// Adjust velocity EACH frame
gatoRigidbody.velocity = new Vector2(velocidadDe$$anonymous$$ovimiento, transform.position.y);
if (is_CanJump)
{
is_CanJump = false;
gatoSaltandoCoroutine = StartCoroutine(SaltoDelGato());
}
}
IEnumerator SaltoDelGato()
{
// Wait for random seconds between $$anonymous$$ and max declared
yield return new WaitForSeconds(Random.Range($$anonymous$$SegundosEntreSaltos, maxSegundosEntreSaltos));
// If grounded then addforce up
if (EstaTocandoElSuelo)
{
gatoRigidbody.AddForce(new Vector2(0, alturaDeSaltoGatos), Force$$anonymous$$ode2D.Impulse); ;
}
is_CanJump = true;
}
Basically, I just added another boolean that represents ability to jump so the grounding won't be related to coroutine call, but only for Impulse inside Coroutine itself.
Let me know if that helps and I understood you correctly.
Follow this Question
Related Questions
2D movement with transform.translate and collision detection...? 1 Answer
Multiple Cars not working 1 Answer
OnCollision2D will not work for me 1 Answer
Bullet hit the collider of a child object 1 Answer
Distribute terrain in zones 3 Answers