I want to add some particle effects for when I die so the player appears to explode upon death, but I am not totally sure how I can stop the player from respawning long enough for this effect to carry out. I currently have some code that just reloads the scene when you die, but I was fiddling with the idea of some way to make the script wait a number of second before executing the respawn method. I saw something in another post on the unity forums about a coroutine, but I am not sure how to make that work as I am pretty new to coding. Any help is greatly appreciated.
-
1\$\begingroup\$ "I saw something in another post on the unity forums about a coroutine" that's pretty much what you need. There are example's of it in the Doc's. "but I am not sure how to make that work as I am pretty new to coding" In that case you may want to Head over to MSDN to get started in C#. \$\endgroup\$– Aditya YadavCommented Jun 2, 2017 at 5:03
-
\$\begingroup\$ I came for help with the task at hand. While your resources are indeed useful, my plan is to learn along the way, and the people here and on the unity community are my resource for that. I want to learn through other humans giving me "live" information tailored to my specific problem. \$\endgroup\$– SencoredCommented Jun 2, 2017 at 5:22
-
4\$\begingroup\$ @Sencored : For very common questions such like yours, I think it's better to take 5 minutes to make a Google search instead of waiting for others to give you the answer you are looking for. It's time to be a strong and independant adult. \$\endgroup\$– HelliumCommented Jun 2, 2017 at 10:11
-
\$\begingroup\$ Of course. I seem to have forgot that this project I am doing for fun is no joke. I will certainly take it more seriously now. Thank you for spreading your wisdom. \$\endgroup\$– SencoredCommented Jun 3, 2017 at 20:54
2 Answers
If you put your "respawn" logic into some method, you can then call it delayed like this: Invoke("respawnMethodName", 5);
, in this case a method named respawnMethodName
will be called after 5 seconds.
You can find documentation on Invoke
here.
-
\$\begingroup\$ Thank you, that is just what I needed. Now the only problem I have is destroying the player object upon death. How do I refer to the player? For example, I have
Destroy(col.gameObject);
in my script for the coins in my game for when you collect the coin, but how would I destroy the player? \$\endgroup\$– SencoredCommented Jun 2, 2017 at 5:20 -
\$\begingroup\$ It depends. If the script that determines when player dies is in player game object, you can do
Destroy(gameObject);
. If script is not in player game object and you don't have a reference to player, you can find player by tag and doDestroy(GameObject.FindWithTag("Player"));
(in this case player object has to have tag set to "Player" in inspector). \$\endgroup\$ Commented Jun 2, 2017 at 5:38 -
\$\begingroup\$ The script is on my player game object, if that is what you mean. I just tried you
Destroy(gameObject);
and forgot that my main camera was parented to my main character, so it destroyed the camera and broke the game. I was trying to avoid it for as long as possible, but I guess it is time for me to write a camera follow script. \$\endgroup\$– SencoredCommented Jun 2, 2017 at 5:44 -
\$\begingroup\$ If you really don't want to use camera follow script (you should), for now you can do
Camera.main.transform.parent = null;
before destroying the player and then reset camera's parent after player is respawned. \$\endgroup\$ Commented Jun 2, 2017 at 5:48 -
\$\begingroup\$ Too late now. lol I Just made one. It isn't too bad. Around 20 lines of actual code not including references and such. Now my only problem is the colliders I have separately parented. I can play with that though. Thanks a million for the help. \$\endgroup\$– SencoredCommented Jun 2, 2017 at 5:57
I don't know how you implemented the respawn mechanic, but if you make it a separate function, you can just call an Invoke with a set amount of seconds so that the method will be called at a later time.
Example usage:
public class ExampleClass : MonoBehaviour {
void OnDestroy() {
Invoke("RespawnPlayer", 10); //this will call the method after 10 seconds
}
void RespawnPlayer()
{
//input what you normally do when you respawn a player.
}
}
If you don't destroy your player object, then you can just call the method right after the "player death" situation. It's essentially the same thing.