Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Advice on a player script

Discussion in 'Scripting' started by AndyNeoman, Jan 8, 2015.

  1. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Any pointers on the best way to create a new player after death.

    My game design has five different characters. initially you have one and need to get coins in game to unlock the next one.

    If you you unlock it before you die then it acts as a another life otherwise it is game over.

    I'd appreciate any tips on the best way to script this in C#. Each 'life' is a unique character prefab.
     
  2. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Thinking off an list players.

    player of players in the list and then adding one to int playerLives that is the counter for the list position.

    On death playerlives++ if upgrade coin total is reached. hopefully get a chance to try this later any tips greatly appreciated.
     
  3. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    So characters are like extra lives? So if you have characters A,B and C (A being the free one) and when you die while playing as C, you get a new live as B? and if you die again it goes to A?

    In this case you could probably have an array for characters. Everytime you unlock a character set a bool value to true. When you die you could just check for highest available character by looping through the array.
     
    AndyNeoman likes this.
  4. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Yes that is the idea, although the char will be playable (unlocked) if the player has gained enough coins in play.

    I am having problems with the array at the moment for some reason, the first player is working fine but I have a condition players == null which is not working, i think i need to add the bool you are suggesting.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,503
    You could give your basic player script a "NumberOfRespawns" integer variable.

    You could preload this whatever number of respawns you want. In the 1980s we used three or four. :)

    Now when you die, do not destroy the player object, but rather do a sequence of steps like this:

    - disable control and movement of the player
    - hide the graphical portion (or make it fall to the ground animation-wise)
    - wait a period of time and play some death rattles
    - check the variable above (NumberOfRespawns)
    - if it is zero, go to game over
    - otherwise, resurrect the player, reenabling his controls, turning back on his graphics, etc.
     
    AndyNeoman likes this.
  6. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Awesome thanks -the only issue is the character is different each time.

    To give a few more details it's 5 different dragons who your are trying to get 'home'.

    You start with one and then the gameplay is to get enough coins to rescue the rest. Really you never want to see the other dragons (unless you pause and go to menu to switch once you have unlocked)

    If your player dies then he cannot make it home ;-( but if you have other Dragons then they can take over the journey to get home.

    Hope my rambling makes sense, I will have a coffer and post some snippets of code as I am having problems getting the next dragon to spawn. The first one works fine so the array is there.

    I was wondering if its because i am using the same prefab currently while waiting for the other dragon art?
     
  7. ezjm427

    ezjm427

    Joined:
    May 17, 2014
    Posts:
    100
    Keep your player script separate (in an empty gameObject) from the 'character' that is getting deleted when he dies - the player script can refer to the gameObject holding the character using a reference and so when it is deleted the reference is set to a new charater (new gameobject/script) and it doesn't affect the code.

    You could also attach the playerScript to the camera, and keep the player/character as a child of the camera
     
    AndyNeoman likes this.
  8. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Yes,
    Argh, so I am deleting the script when I destroy the object. Thanks I will try that.
     
  9. ezjm427

    ezjm427

    Joined:
    May 17, 2014
    Posts:
    100
    Yeah it's a nasty (sometimes helpful) side effect of Destroy( GameObject ); it destroys all components of that gameObject including script, as well as any children currently attached under the GameObject set in the Unity hierarchy or through code.

    Yes, there is a way to change an GameObject's mesh during game, but the way Unity works I think it would be easier to keep the characters prefab separate from the script and just delete/create the prefabs when youre deleting/adding the new char and set a reference in your playerScript let's say gobj_player and change that reference so it always refers to the 'newest' created character prefab.

    How you determine which character is created is up to you - you could keep a running int charCount=0 in the playerScript and create a new char based on what value that is, but it really depends how your coding.

    You could even create a reference of List in the playerScript that refers to the characters
    Code (CSharp):
    1. List<GameObject> charTypes = new List<GameObject>();
    2. int charCount = 0;
    3. GameObject gobj_Player;
    4. void Start()
    5. {
    6.      charTypes.Add (Resources.Load<GameObject>("Prefabs/char1"));     //charType[0]
    7.      charTypes.Add (Resources.Load<GameObject>("Prefabs/char2"));    //charType[1]
    8.      charTypes.Add (Resources.Load<GameObject>("Prefabs/char3"));    //charType[2]
    9.  
    10.     gobj_Player = GameObject.Find("charFirst"); //Create a 'first gameobject char in the scene,                                                                                   load the rest
    11.     //Instead of finding one you could instantiate here (or set it in the inspector in the scene)
    12. }
    13.  
    14. void loadNewChar()
    15. {
    16.          Destroy ( gobj_Player );
    17.          gobj_Player = (GameObject)(Instantiate( charTypes[ charCount ] ) );
    18.          gobj_Player.transform = transform; //Change new object to match position and rotation
    19.          //if you want to make the gobj_Player a child of the playerScript/cam, do it here
    20.          charCount++;
    21.          //Add code here to change charCount if the value gets too high for charTypes list
    22. }
     
    Last edited: Jan 9, 2015
    AndyNeoman likes this.
  10. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938

    Thanks for that.

    I am still struggling to implement currently but I have decide to go for an array as I know the number of players.

    Is it possible to instantiate element one of array wait for that to be destroyed then instantiate element 2? checking first if the game has put a true element in there?
     
  11. ezjm427

    ezjm427

    Joined:
    May 17, 2014
    Posts:
    100
    The array I made stores references to prefabs - which are not GameObjects that actually exist, but you can duplicate prefabs from the array using Instantiate - which will create a Gameobject duplicated of that prefab - so the List/Array is not the actual characters, but their prefab/type to be copied through Instantiate

    And when you instantiate, the new GameObject created from the prefab data is stored in gobj_player in my code
     
    AndyNeoman likes this.
  12. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Sorry, I did try to reply the other day but for some reason my account was being funky and it had to be verified. I solved this via an array with a static int moving along it each time bool for dragons death was set to true.

    Is only the first fix as I want to call other methods now to show death etc but it is a good starting point.

    Cheers for all the advice :)