Alex wanted me to create a gui that comes up when the game starts and prompt the user what he wanted to use: keyboard/mouse or xbox controller. When that is fleshed out, he wants to be able to hit a key that will pause the game with a menu that has a button that will let him continue the game. I researched on how to create a gui and found resources on using unity to create buttons to us for the gui for the time being. To use the gui in unity, all gui must be under the
function OnGUI()
{
}
The function OnGUI() works like function Update() so it will keep checking the script every frame. The gui script im using functions like
GUI.Button(shape(position), "text"))
In the example that was shown to me youtube, the position was determined by division of the screen. So if you divide the screen in half:
GUI.Button(Rect(Screen.width/2 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "text")
it should place the button in the middle of the screen. I needed two buttons that are able to set joystick variable accordingly. Only way to do this is to have the script call the variable from the player script and set it. So I immediately changed the var joyStick to a static var joyStick. Now the other scripts can call that script and made changes.
The tutorial showed how to make the button clickable so we could add if/else statements in them. Just simply add if in the beginning and place a bracket around everything else.
if(GUI.Button(Rect(Screen.width/3 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "Keyboard/Mouse"))
this should let me be able to click it. So I created a startPrompt script to show up immediately when the game start.
Function OnGui()
{
if(GUI.Button(Rect(Screen.width/3 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "Keyboard/Mouse"))
}
Now I need to add the statements. Alex wanted the script to be able to set the joystick to either keyboard/mouse or xbox controller. So having already set the joystick to static in the playerControl script, I need to add a statement and create another IF statement.
private var buttonWidth:int = 125;
private var buttonHeight:int = 50;
private var spacing:int = 100;
Function OnGUI()
{
if(GUI.Button(Rect(Screen.width/3 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "Keyboard/Mouse"))
{
PlayerControls.joyStick = 0;
Destroy(this);
}
if(GUI.Button(Rect(Screen.width/3 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "Keyboard/Mouse"))
{
PlayerControls.joyStick = 1;
Destroy(this);
}
So now when the game start, it will prompt the user with 2 buttons. One will set the joystick controls to keyboard/mouse, the other to the xbox controller. I added the destroy in the script so it will end it when the player decide on controls. Otherwise the player will constantly be prompt. The private var is there to define the dimension of the button because we used it in the script.The next problem I ran into is the game isn’t paused and you’re still able to do everything else. I found a timescale script that could reduce game speed down to 0, stopping everything from playing or something of that nature. The script also needed to be the first thing to work before everything else. Using the timescale, I was able to freeze the game(pause) and wait for the player to pick controls and then continue(this is where the destroy script helps make this function work the way I want). First I wanted the game to stop so I placed the timescale at the top of the script in the OnGUI function.
private var buttonWidth:int = 125;
private var buttonHeight:int = 50;
private var spacing:int = 100;
Function OnGUI()
{
Time.timeScale = 0.0;
if(GUI.Button(Rect(Screen.width/3 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "Keyboard/Mouse"))
{
PlayerControls.joyStick = 0;
Time.timeScale = 1.0;
Destroy(this);
}
if(GUI.Button(Rect(Screen.width/3 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "Keyboard/Mouse"))
{
PlayerControls.joyStick = 1;
Time.timeScale = 1.0;
Destroy(this);
}
}
With the timescale in place, it will pause immediately then prompt with the two buttons. The numbers used to divide the screen will position the buttons so they don’t overlap each other. Now I have to set up an input key so it pauses in the game, pop up a menu button that allows the player to continue the game. I looked up on key inputs and the script that looked best to use is Input.GetKey(KeyCode.key) key is which key you want to set it up to use as input. Theres a list that showed which could be used.
private var buttonWidth:int = 125;
private var buttonHeight:int = 50;
private var spacing:int = 100;
static var paused = false;
function OnGUI()
{
If(Input.GetKey(KeyCode.Escape))
{
Paused = true;
Print(“paused”);
Time.timeScale = 0.0;
}
If(paused == true)
{
Time.timeScale = 0.0;
if(GUI.Button(Rect(Screen.width/2 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "Continue"))
{
Paused = false;
Time.timeScale = 1.0;
}
}
}
It should pause the game when you hit escape the script would set paused to true and trigger the if statement to bring up the button. After testing a few times, I noticed when I hit the escape during the startprompt the ingame pause function would come up. I have to set up a variable for the menu script to only work if the startprompt is done.
private var buttonWidth:int = 125;
private var buttonHeight:int = 50;
private var spacing:int = 100;
static var paused = false;
static var startPrompt = false
function OnGUI()
{
If(Input.GetKey(KeyCode.Escape))
{
Paused = true;
Print(“paused”);
Time.timeScale = 0.0;
}
If(paused == true && startPrompt == true)
{
Time.timeScale = 0.0;
if(GUI.Button(Rect(Screen.width/2 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "Continue"))
{
Paused = false;
Time.timeScale = 1.0;
}
}
}
In the startprompt script, I added the startprompt line to each button if statement to be set to true when one of the controls is picked. The only thing I didnt do was set a joystick input to bring up menu in game because i dont have a controller to test. After testing a few times, the ingame pause menu doen’st show up when you’re still deciding on controller but it still shows up after the controls are pick if you press the escape during the control pick. So the timescale itself is not a true pause. I need to find a real pause function that stops other scripts from doing anything.
No comments:
Post a Comment