Search Unity

enemy respawning and other stuff

Discussion in '2D' started by z3, Jul 25, 2016.

  1. z3

    z3

    Joined:
    Jun 3, 2016
    Posts:
    34
    So I'm new to unity and C# I went through a 2d platformer tutorial by gamesplusjames on youtube
    to make a basic game but now that i finished that i need some help with what direction i should go for a few things.

    1) in the tutorial he just makes a enemy and places them in the level which is fine but lets say I pass a checkpoint and then die when i respawn all the enemies I've killed are gone which makes for a boring retry from the spawn point so I know Ill need prefabs of them I have figured out how to make those I'm looking for a tutorial or directions on respwaning the ones that were killed if the player dies

    2) I'm guessing to keep track of the coins, lives, ect that the player has collected through multiple levels I'll need to do a game manager script and from what I have read and have it load before all my levels ?? but i also read this doesn't work in the editor when you hit play so if anyone knows like the simplest tutorial for that with mabey a example of how it would keep track of say amount of coins that would be great.

    Thank you
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I don't understand your question 1, but for question 2, just use PlayerPrefs. It's easy: just call PlayerPrefs.GetInt("coins") to get the current number of coins, and PlayerPrefs.SetInt("coins", qtyCoins) to store a new quantity of coins.
     
  3. PeaceLaced

    PeaceLaced

    Joined:
    Jun 2, 2015
    Posts:
    53
    For question 2, watch this video, it goes into a simple game manager if that is what you are interested in doing.

    For question 1, my suggestion would be to use triggers of some sort to spawn enemy prefabs. The video I linked above uses an invisible trigger to detect if your player is on or off the ground. Something similar could be set up at the respawn position that basically says, when player enters "this trigger", spawn enemies at, "this point, this point, and this point".

    Something I like doing is randomizing the spawning of objects, so you could do something like this inside the trigger:
    Code (CSharp):
    1. public float mobPositionX;
    2. public float mobPositionY;
    3.  
    4. public int numberOfMobs = 3;
    5.  
    6. while (numberOfMobs > 0)
    7.   {
    8.  
    9.   Vector3 mobPosition = new Vector3(Mathf.Floor(Random.Range(mobPositionX - 5, mobPositionX + 5)), Mathf.Floor(Random.Range(mobPositionY - 5, mobPositionY + 5)), 0);
    10.   GameObject mobSpawn = Instantiate(yourMobPrefab, mobPosition, Quaternion.identity) as GameObject;
    11.  
    12.   numberOfMobs = numberOfMobs - 1;
    13.  
    14.   }
    15.  
     
    Last edited: Jul 25, 2016