Search Unity

Help delay my death function!

Discussion in 'Scripting' started by Eekul, Jan 19, 2019.

  1. Eekul

    Eekul

    Joined:
    Jan 15, 2019
    Posts:
    7
    If any of the frequenters of the forums happen to drop in on this post; beware I will be having a lot of questions! I'm a new programmer who is interested in game design, and I tend to learn best by 1.) Attempting to problem solve on my own, 2.) Researching the problem 3.) Asking others with more experience for help.

    I learned how to delay "Die" being called by using invoke; however that doesn't allow time for the animation to process, and there is a quick snap of the screen back to the player's original transform. I am looking for a smoother solution than using invoke to delay death. My code is listed in the files below. My ProcessPlayerDeath function lives within my Game Session/Game Manager Script. And my Die function lives within my player controller script.

    I'm thinking there may be a way to delay "ProcessPlayerDeath" using transform and that will solve the issue? However I am simply unsure.

    Thanks in advance for the effort and replies.



    help1.PNG help2.PNG
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,406
  3. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    You learn stuff in a nice way, don't forget to add "4) Answer others with less experience". This actually help you a lot to remember what you learnt, and sometimes help you getting a new idea when you are momentally stuck on something.

    I confirm, coroutines are easy way to add delays.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Testingblabla : MonoBehaviour
    6. {
    7.     IEnumerator DelayedDeath()
    8.     {
    9.         Debug.Log("Hero is dying");//Launch the animation and stuffs
    10.  
    11.         yield return new WaitForSeconds(5f);//Delay for 5 seconds
    12.  
    13.         Debug.Log("Hero is dead");//ProcessPlayerDeath
    14.  
    15.     }
    16.      void Start()
    17.     {
    18.         StartCoroutine("DelayedDeath");//The hero starts to die
    19.      
    20.     }
    21.  
    22. }
    You could also, inside the animation, set a function to be called when the animation reaches a certain time. (At the end of your "Die" Animation, for example)

     
  4. Eekul

    Eekul

    Joined:
    Jan 15, 2019
    Posts:
    7
    Trying to use code to do it first; and I'm having a problem. As soon as I spawn, it triggers an unstoppable chain of deaths,(which do appear to be spread apart by 2 seconds). I'll post the picture of my code. And in my head it should be working properly, but clearly it isn't. Maybe you could give me a hand?

    *PS* I attempted putting the "StartCoroutine" line of code within the Start method as well; This yielded the same results as mentioned above.
     

    Attached Files:

  5. Eekul

    Eekul

    Joined:
    Jan 15, 2019
    Posts:
    7
    UPDATE!!!! I was actually able to figure it out! Using the code as posted below!
     

    Attached Files:

    DJD10 likes this.