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

Vehicle selection screen

Discussion in 'iOS and tvOS' started by JamesArndt, May 23, 2011.

  1. JamesArndt

    JamesArndt

    Unity Technologies

    Joined:
    Dec 1, 2009
    Posts:
    2,912
    I'm trying to wrap my head around setting up a selection screen for vehicles. So I've been doing my research and so far I believe I have to create an array to store a series of vehicles. I need the game to remember the vehicle that's been chosen and spawn that vehicle in any world the user chooses. I really don't know how to full on tackle this technically, but I am finding research that helps here and there. If anyone can give me a small boost with a start, and I can take the whole way (as a learning experience). If anyone would like to help I can offer the compensation of (30) free models or I can model and texture and single asset (low poly) you might need. My selection screen will be in the main menu...it will have 4 basic vehicles on the one screen, a user would be able to click on a vehicle and it will do a turnaround (which I can create my own animations for) and then basically they can return to main menu screen and launch into a level. Ideally their selected vehicle will be the one that spawns at the starting line in any track they choose. If I didn't mention I need help in scripting this functionality.
     
  2. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    Hi, here's how I'd do it:

    In any game I make which uses levels, I tend to make an empty GameObject called "theBrain" which sits on Level0 and has the following script attached so it will persist across all the subsequent levels:

    Code (csharp):
    1. function Awake () {
    2.     DontDestroyOnLoad (this);
    3. }
    I use "theBrain" script to also hold any recalled data from memory, high scores, playerNames, etc.

    Then, once you have loaded any memory data etc. - just advance to Level1 (Main Menu), here you can choose to go to your car selection screen, etc.

    In your selection screen, when the user clicks on a car they want to pick, they could actually just be clicking an invisible GUI button, and when they do so, the button they clicked tells a variable in theBrain's script which car was selected (1,2,3 or 4).

    Now, whenever you move to any track levels, you can easily just ask theBrain which car prefab to instantiate.

    Hope that makes sense?

    Here's a few code snippets which might help (untested, simplified for illustration):

    (Sure you can use an Array to hold the prefabs, but the below code does not go into that)....

    LEVEL 0

    Code (csharp):
    1.  
    2. // Brain Script (must be called BrainScript)
    3.  
    4. // A variable to remember which car the player chose:
    5. var selectedPlayerCar : int;
    6.  
    7. function Awake () {
    8.     DontDestroyOnLoad (this);
    9. }
    10.  
    Level 1

    Code (csharp):
    1.  
    2. // Car Selection Script
    3.  
    4. // Link to the Brain Script:
    5. private var theBrain : BrainScript;
    6.  
    7. function Awake(){
    8.     // Complete the reference to the Brain Script:
    9.     theBrain = FindObjectOfType(BrainScript);
    10. }
    11.  
    12. function OnGUI () {
    13.     // Button 1 to choose Car 1
    14.     if (GUI.Button (Rect (0, 0, 100, 20), "CAR1")){
    15.         // Spin Car 1
    16.        
    17.         // Tell the Brain which car was chosen
    18.         theBrain.selectedPlayerCar = 1;
    19.  
    20.         // Go back to Main Menu
    21.     }
    22.  
    23.     // Button 2 to choose Car 1
    24.     if (GUI.Button (Rect (0, 100, 100, 20), "CAR2")){
    25.         // Spin Car 2
    26.        
    27.         // Tell the Brain which car was chosen
    28.         theBrain.selectedPlayerCar = 2;
    29.  
    30.         // Go back to Main Menu
    31.     }
    32.  
    33.     // etc.
    34.  
    35. }
    36.  
    Level 2

    Code (csharp):
    1.  
    2. // Racing Track Script
    3.  
    4. // Link to the Brain Script:
    5. private var theBrain : BrainScript;
    6.  
    7. function Awake(){
    8.     // Complete the reference to the Brain Script:
    9.     theBrain = FindObjectOfType(BrainScript);
    10. }
    11.  
    12. function Start(){
    13.    
    14.      if (theBrain.selectedPlayerCar == 1 ){
    15.        
    16.         // Instantiate Car 1
    17.  
    18.     }
    19.  
    20.      if (theBrain.selectedPlayerCar == 2 ){
    21.        
    22.         // Instantiate Car 2
    23.  
    24.     }
    25.  
    26. }
    27.  
    Hey, I'm not saying this is the only way to do this, but it's the easiest to explain! :razz:
     
  3. BloodGreen Studio

    BloodGreen Studio

    Joined:
    Dec 16, 2010
    Posts:
    48
    I can help. Ill send an example project of what I use when I finish my homework...
    I dont really use and array to select them. It takes a little longer to set up but it works.

    Edit: oh we posted at the same time. Hey James if you still want it tell me, but RobbieDingo's is pretty similar
     
    Last edited: May 23, 2011
  4. NT7Games

    NT7Games

    Joined:
    May 21, 2009
    Posts:
    43
    James,

    You'll need some good-looking UI to present the cars, probably in a spinner or line-up of cars where the "active" car is scaled up a little more than the others, a la Crusin USA or some other coin-op racing game. You'll need to track what is the active car and store is once the user has made a selection... Then, you'll need to be able to access that car throughout the game, within other levels, etc. So the selected car needs to be stored rather permanently.

    I would use static variables in my "CarSelection" scripts so that the selected car can be accessed by (and potentially changed by) any other script. So, after the car is selected, you could write (and yeah, this is pseudocode...):

    Code (csharp):
    1. CarSelection.selectedCar = <the car the user selected. selectedCar is a GameObject>;
    later, when you want to Instantiate said car in some other script - say, level 1, you would call that car like this:

    Code (csharp):
    1. Instantiate (CarSelection.selectedCar, Vector3.zero, Quaternion.identity);
    Hope this helps.
     
    Last edited: May 24, 2011
  5. JamesArndt

    JamesArndt

    Unity Technologies

    Joined:
    Dec 1, 2009
    Posts:
    2,912
    I actually have a working menu, (here's a vid of it in action - http://www.youtube.com/watch?v=1leWU2VWSgM&feature=channel_video_title ) just need to place in my (4) vehicle objects and some menu behaviors on them....I'm just to the point of creating the part where a user actually touches on a vehicle and it selects and stores it for the user....ideally when then select a track to race it would start with their selected vehicle. I wanted to initially create it to where a user could swap to a more customized texture on their vehicle, but the scripting aspect is holding me back on that aspect as well right now. All of the help in this thread is amazing...cannot believe how awesome you all are to help me. I most definitely must pay back this forum in any way I can help. I'm also thinking giving free versions of my final game to folks who have helped me in it's creation :)
     
  6. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    Cheers James.

    To do a texture swap, just have an array of the materials (assigned in the inspector) and do the same thing as I suggested above - ie. use the brain to remember the integer which will point to the position in the array.

    Good luck!

    R
     
  7. gypsy fly

    gypsy fly

    Joined:
    Mar 31, 2011
    Posts:
    36
    I learned to do character selection from the Unity Tutorial by the same title. Techniques ought to be similar to vehicle selection with texture swaps.
     
  8. JamesArndt

    JamesArndt

    Unity Technologies

    Joined:
    Dec 1, 2009
    Posts:
    2,912
    Gypsy - Where did you see the Unity Character Select tutorial by chance?
     
  9. defjr

    defjr

    Joined:
    Apr 27, 2009
    Posts:
    436
  10. JamesArndt

    JamesArndt

    Unity Technologies

    Joined:
    Dec 1, 2009
    Posts:
    2,912
    Sweet baby Jesus.....this should definitely set me on the right path. Thanks Earl, thanks everyone!


    *Actually I've looked this over and I think it's getting ahead of me a bit...I still need to establish a screen with 4 vehicles to choose from, selecting one and then the game remembering my vehicle and loading it into whatever level I choose to play. I'm still gnawing away at all these things. I need like an idiot's guide for real lol.
     
    Last edited: May 25, 2011
  11. JamesArndt

    JamesArndt

    Unity Technologies

    Joined:
    Dec 1, 2009
    Posts:
    2,912
    I actually found a free project for Unity 3.0 called CarEdu that might actually have everything I need in it...it's like a car racing game tutorial project with main menu functionality in it as well....thought I'd share if anyone else needed a resource like this.
     
  12. Vipul-Dudharejiya

    Vipul-Dudharejiya

    Joined:
    Nov 17, 2014
    Posts:
    2
    Please share that tutorial because i am also stuck at the same problems from selecting the vehicle ...
     
  13. Davidbillmanoy

    Davidbillmanoy

    Joined:
    Jul 7, 2014
    Posts:
    119
    How can I do it to Prefabs(Cars)?
     
    Last edited: Feb 15, 2015