Search Unity

Choosing a player?

Discussion in 'Scripting' started by All_American, Oct 31, 2011.

  1. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Say I have 2 or 3 or 4 different character meshes the player can choose from before they start the game.

    Would I just duplicate the level however many times as different players and load that characters level when they push the button to choose- or is there a better way to do this.....for android??
     
  2. badbii

    badbii

    Joined:
    Dec 6, 2009
    Posts:
    83
    and why don't you save player(-s) in prefabs?
     
  3. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    All my character meshes are prefabs....
     
  4. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Also, use PlayerPrefs. :)
     
  5. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
  6. Tazadar

    Tazadar

    Joined:
    Oct 1, 2011
    Posts:
    91
    Use Instanciate ?
     
  7. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Can you elaborate???
     
  8. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    If you have four different characters, and they are created as prefabs, you would set up a script that has a reference to all of those prefabs stored in an array. Then when you start your game, you can figure out which character they want to play, and use the Instantiate function to create a clone of the prefab.

    Something like this (note: untested code):

    Code (csharp):
    1.  
    2. var characters[];
    3. var characterChosen : int;
    4. var playerCharacter;
    5. function Start()
    6. {
    7.    playerCharacter = Instantiate(characters[characterChosen], Vector3.zero, Quaternion.identity);
    8. }
    9.  
    10.  
    Set the array of characters to refer to your prefabs, then, at level load, you set the characterChosen integer to the index of the character you want to play.
     
    Last edited: Oct 31, 2011
  9. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I understand it a little better- I do have them as prefabs so I would give them the int value1-4 for each of them and then that value would load the right one at run time??

    Is this the same in C# sept for the function

    Not sure I get the array deal-
     
    mattis89 likes this.
  10. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    Ah, okay, sorry, C# is easy enough to convert to. For some reason I thought you were using UnityScript.

    Code (csharp):
    1.  
    2. class SelectPlayer: MonoBehaviour
    3. {
    4.   public GameObject[] PossibleCharacters;
    5.   public int SelectedCharacterIndex;
    6.  
    7.   [HideInInspector]
    8.   public GameObject playerCharacter;
    9.   void Start()
    10.   {
    11.     playerCharacter = Instantiate(PossibleCharacters[SelectedCharacterIndex], Vector3.zero, Quaternion.identity);
    12.   }
    13.  
    14. }
    15.  
    16.  
     
  11. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Where would I put this code at In the camera where my main menu is???
     
  12. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    Hmm, that will depend on how you handle your character selection. If you DO NOT perform a level load between character selection screen and playing the game, you can attach that script anywhere you like, camera could easily work for it, or perhaps put it on whatever game object you have designated as your game controller. If you load a new level between character selection and playing the game, you either want to save which character was selected in to player prefs so that it persists through the level load, or attach the script to an object that is flagged DontDestroyOnLoad.
     
  13. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Wow, I am opening up a can of worms now- I don't even know what playerprefs are or what they are for yet.....

    Ok Justin I appreciate the insight, I need to get on this playerpref deal eh?
     
  14. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    Probably not.

    I would set the character select to be in the same scene as the main game for now, later on, solve the problem of how you get the player's selection of character to play with in a menu scene passing through, but it's not a problem you need to solve right now. The simplest solution, when you get that far, would actually be to create a persistent object in your character select scene that remembers the index of the character selected, and then in your game play scene, you just find that game object, then ask it what the selected character index was.
     
  15. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    Here's a quick character selection screen I just threw together. It displays buttons based on the number of pre-defined characters. I created four nefarious prefab characters we shall introduce as Mr Sphere, Mr Cube, Mr Cylinder and Dr Capsule. The first scene to load is the character select scene, upon selecting your hero, the game play scene loads, with the character you have selected.

    All code and designs for this character selection are released to the public domain so you are free to do with them as you please.
     

    Attached Files:

  16. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Thanks Justin I will be figuring this out today, I need to wrap my head around this. Thanks for pointing me in the right direction in doing this-
     
  17. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Justin-

    I've been working with this character select package, I see what it is suppose to do, but it isn't picking the players.

    Not sure if I am doing it right thou. My players are prefabs and if I do not put them int he scene nothing loads- If I put them both in the scene they both load no matter what.

    I'm I missing something??
     
  18. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Anyone have clues why this won't work????
     
  19. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    This is what I am trying since I can't figure the above out-

    Code (csharp):
    1. void OnGUI ()
    2.     {
    3.         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), BackGround);
    4.               GUI.color = Color.green;
    5.           if (GUI.Button(new Rect(30, 100, 200, 50), "Fly the F-35 JSF"))
    6.             {
    7.             Instantiate(JSFPlayer, transform.position, transform.rotation);
    8.             AudioSource.PlayClipAtPoint(clip,currentListener.position);
    9.             Application.LoadLevel(1);
    10.             }
    11.         if (GUI.Button(new Rect(30, 200, 200, 50), "F-16E"))
    12.             {
    13.             Instantiate(FightingFalconPlayer, transform.position, transform.rotation);
    14.             AudioSource.PlayClipAtPoint(clip,currentListener.position);
    15.             Application.LoadLevel(1);
    16.             }
    17.     }
    Why wouldn't this work- this is in the main menu scene, then loads to the level but the Instantiation isn't happening.
     
  20. rokstar234

    rokstar234

    Joined:
    Mar 29, 2011
    Posts:
    94
    check if the characters inst null. also you instantiating , then making the game load a different level without you char. the instantiating is working, its just getting destroyed when you switch. so you need to instantiate after the level is loaded. to do this you need you will need to make sure this script or object isn't destroyed when you change scenes, to do this you add the DontDestroyOnLoad() function to one of your methods ( preferably start or update). then you want to make a variable that will store your selected character. and make it store that character into the variable when you hit those buttons you defined. then load the next level, now call a function after its done to make it check what level your on(using if statements) if true instantiated the character stored in your variable. and that should work :)
     
  21. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    -
     
    Last edited: Nov 6, 2011
  22. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    -
     
    Last edited: Nov 6, 2011
  23. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    This is what I have so far-

    Not sure how to store the variables to load in the level thou-

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MainMenu : MonoBehaviour {
    5.    
    6.     public AudioClip clip;
    7.     public Transform currentListener;
    8.     public Texture BackGround;
    9.     public GameObject JSFPlayer;
    10.     public GameObject FightingFalconPlayer;
    11.    
    12.     void Awake()
    13.     {
    14.         DontDestroyOnLoad(this.JSFPlayer.gameObject);
    15.         DontDestroyOnLoad(this.FightingFalconPlayer.gameObject);
    16.     }
    17.     void Start ()
    18.     {
    19.          currentListener = GameObject.Find("Audio").GetComponent<Transform>();
    20.     }
    21.     void OnGUI ()
    22.     {
    23.         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), BackGround);
    24.               GUI.color = Color.green;
    25.           if (GUI.Button(new Rect(30, 100, 200, 50), "Fly the F-35 JSF"))
    26.             {
    27.             Instantiate(JSFPlayer, Vector3.zero, Quaternion.identity);
    28.             AudioSource.PlayClipAtPoint(clip,currentListener.position);
    29.             Application.LoadLevel(1);
    30.             }
    31.           if (GUI.Button(new Rect(30, 200, 200, 50), "F-16E"))
    32.             {
    33.             Instantiate(FightingFalconPlayer, Vector3.zero, Quaternion.identity);
    34.             AudioSource.PlayClipAtPoint(clip,currentListener.position);
    35.             Application.LoadLevel(1);
    36.             }
    37.     }
    38.     void Update()
    39.     {
    40.           if (Input.GetKey(KeyCode.Escape))
    41.             {      
    42.             Application.Quit();
    43.             }
    44.     }
    45. }

    This forum is whacked...........
     
  24. rokstar234

    rokstar234

    Joined:
    Mar 29, 2011
    Posts:
    94
    you mistook what i said, i said store the charater into another varable

    e.g

    public GameObject SelectedPlayer;

    and this too:
    GameObject PLinScene

    also add the line where the others are:

    DontDestroyOnLoad(gameObject);

    then in you Gui method when you loaded the level assign the SelectedPlayer.
    Like this:

    SelectedPlayer = JSFPlayer;

    now make a function that will load your level then instantiate you character after the level is loaded like this:

    void MyInstantiateFunction(bool LoadLevel = false) {
    GameObject PLinScene = GameObject.FindObjectsWithTag("Player")



    if (Application.LoadedLevel == 1 PLinScene == null) {

    Instantiate(SelectedPlayer, transform.position , Quaternion.identity );
    }
    if( LoadLevel == true) {

    Application.LoadLevel(1);
    }

    now to to make sure it will instantiate when the level loads call this function without putting any parameters in Start():

    MyInstantiateFunction();

    now to clean up OnGUI() and put in the Function call:

    void OnGUI ()
    {
    GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), BackGround);
    GUI.color = Color.green;
    if (GUI.Button(new Rect(30, 100, 200, 50), "Fly the F-35 JSF"))
    {
    SelectedPlayer = JSFPlayer;
    AudioSource.PlayClipAtPoint(clip,currentListener.position);
    MyInstantiateFunction(true);
    }
    if (GUI.Button(new Rect(30, 200, 200, 50), "F-16E"))
    {
    SelectedPlayer = FightingFalconPlayer;
    AudioSource.PlayClipAtPoint(clip,currentListener.position);
    MyInstantiateFunction(true);
    }
    }

    you may get a few errors with this.... reason being i cod3ed this in the forum, not any proper compilers which i dont have on this PC
     
  25. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I'm kind of confused- what am I supposed to put in the level?

    That code I posted is in my main menu - do I need to make a cs script that sits in the first level??
     
  26. rokstar234

    rokstar234

    Joined:
    Mar 29, 2011
    Posts:
    94
    actually yeah thats a good idea, that what you should you. make a level Manager or gameManger class ill get you started with:

    now make an empty and call it whate ever you want then add this script:
    now replace your Main Menu script with this one

    i tested this and it works, i get a null reference error because you have you listener else where and it cant find it. you wont get the error or should any way
     
    Last edited: Nov 6, 2011
  27. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Ok- now I am getting somewhere-

    This works, but there is an issue- when the player is instantiated it just keeps instantiating one every frame- so I end up with 50 as soon as the level starts-
     
  28. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Rokstar I am having an issue with the GameManager(EmptyOBject) in the geme level being instantiated again if you restart the level which is jacking up the stats at the end of the level- basically overlapping the 2 sessions ontop of eachother-

    Is there a way to destroy it when the level ends without destroying it for good??

    Also if the player loses or wins- when going to the lose or win scene the players character is being instantiated there also- any way to stop that from happening??
     
  29. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I fixed the manager being re-instantiated on every new scene entry by deleting this out of the manager.cs

    Code (csharp):
    1.  
    2. void Start () {
    3.  DontDestroyOnLoad(this);
    4.  }
    5.  
    6.  // Update is called once per frame
    7.  void Awake()
    8.  {
    9.  DontDestroyOnLoad(this.gameObject);
    10.  }
    11.  
    I also had to create a separate win and lose scene for each character- I am thinking if I should just use one player.cs for all players, right now I have separate .cs for each player- if I use the same one will that stop me from needing separate win and lose scenes with the stats???
     
  30. rokstar234

    rokstar234

    Joined:
    Mar 29, 2011
    Posts:
    94
    ok that's weird, it instantiated every frame for me, then i fixed it?, also yeah there is a way, that is you destroy it then re instantiate it, or make it check for the same player, if null then instantiate. as for your win-lose scenes it really depends if there different from each-other, if there more or less the same it shouldn't matter, you can make those script all inherit form your player, it shouldn't impact your game system, unless its more complex than i think it is.