Search Unity

Randomizing a DeathKick.

Discussion in '2D' started by Eekul, Jan 23, 2019.

  1. Eekul

    Eekul

    Joined:
    Jan 15, 2019
    Posts:
    7
    Upon dying, I have a Vector2 Deathkick set to 10f, 10f. However, I would like to be able to have this value a little bit more random, within certain parameters. I'm unsure on how to set this, any help would be appreciated!
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    new Vector2(UnityEngine.Random.Range(-20f, 20f), UnityEngine.Random.Range(-20f, 20f))
     
  3. Eekul

    Eekul

    Joined:
    Jan 15, 2019
    Posts:
    7
    Thanks a lot Primoz! I got the code working :)
     
  4. Eekul

    Eekul

    Joined:
    Jan 15, 2019
    Posts:
    7
    For anyone curious, I used the following code in order to get this to work:

    IEnumerator Die()
    {
    if (myBodyCollider.IsTouchingLayers(LayerMask.GetMask("enemy", "harmfulObjects")))
    {
    Vector2 deathKick = new Vector2(UnityEngine.Random.Range(-10f, 10f), UnityEngine.Random.Range(10f, 10f));
    isAlive = false;
    myAnimator.SetTrigger("Die");
    GetComponent<Rigidbody2D>().velocity = deathKick;
    yield return new WaitForSeconds(1.5f);
    FindObjectOfType<GameSession>().ProcessPlayerDeath();
    }

    It should also be noted that I do not believe I'm allowed to serialize a random variable, therefore I moved the variable within my "Die" Coroutine.