Redirecting you to bearmacstudios.com.... BearMac Studios: Scripts

Pages

Scripts

Vignette/HUD Overlay Script
A small script that overlays a texture over the entire screen

var vignette2D : Texture;

function OnGUI() {
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height), vignette2D , ScaleMode.StretchToFill, true);
}

Sprint Script

Just a little script that makes the default FPS controller in unity have a sprint function.

//Sprinting with the default fpscontroller in Unity 3.0

var sprintSpeed : float = 15;
var defaultSpeed : float = 10;

//sets default run speed
function Start () {
 GetComponent(CharacterMotor).movement.maxForwardSpeed = defaultSpeed;
 }
//When the sprint button is pressed, change speed to sprint
function Update () {
if (Input.GetButtonDown("Sprint")){
  GetComponent(CharacterMotor).movement.maxForwardSpeed = sprintSpeed;
  }
//When the sprint button is let up, change back to walk speed
if (Input.GetButtonUp("Sprint")){
 GetComponent(CharacterMotor).movement.maxForwardSpeed = defaultSpeed;
}
}

Main Menu Script

Here's the main menu script. Take a look, ask any questions in the comments.

//mainmenu.js by mactinite 
//Simple main menu
var areaHeight : float= 200;
var areaWidth : float= 200;
var layoutCenter : float = 100;
var firstLevel : String = "First";
var buttonSpacing : float = 25;
var customSkin : GUISkin;
var layoutStyle : GUIStyle;
var textureTop : Texture2D;
function OnGUI(){
GUI.skin = customSkin;
GUILayout.BeginArea(Rect(Screen.width/2-areaWidth/2,Screen.height/2-areaHeight/2,areaWidth,areaHeight),layoutStyle);
GUILayout.Space(25);
GUILayout.Label(textureTop);
GUILayout.Space(buttonSpacing);
 if(GUILayout.Button("Start Game")){
 Application.LoadLevel(firstLevel);
 }
GUILayout.Space(buttonSpacing);
 if(GUILayout.Button("Quit")){
 Application.Quit();
 }
GUILayout.EndArea();
}

Pause Menu Script


Here's a script that's extremely similar to the main menu script. It's for the pause menu in the tutorial that's on YouTube right now!

var menuHeight:float=500;
var menuWidth:float=500;
var buttonSpacing:float=25;
var mainMenu: String = "MainMenu";
var titleTexture:Texture2D;
var customSkin:GUISkin;
var customStyle:GUIStyle;

function OnGUI(){
GUI.skin = customSkin;
GUILayout.BeginArea(Rect(Screen.width/2-menuWidth/2,Screen.height/2-menuHeight/2,menuHeight,menuWidth),customStyle);
GUILayout.Space(50);
GUILayout.Label(titleTexture);
GUILayout.Space(buttonSpacing);
if(GUILayout.Button("Main Menu")){
Application.LoadLevel(mainMenu);
}
GUILayout.Space(buttonSpacing);
if(GUILayout.Button("Exit to Desktop")){
Application.Quit();
}
GUILayout.Space(buttonSpacing);
GUILayout.EndArea();
}

Here's the Pause Controller script if you guys didn't make it from the tutorial:

var gamePaused : boolean = false;
function Update () {
 if(Input.GetKeyDown(KeyCode.Escape)){
  if(gamePaused){
  Time.timeScale=1;
  gamePaused = false;
  gameObject.GetComponent(PauseMenu).enabled = false;
  }
  else{
  Time.timeScale = 0;
  gamePaused = true;
  gameObject.GetComponent(PauseMenu).enabled = true;
  }
 }
}

13 comments:

  1. Hey, I couldn't comment on Youtube for some reason, so here goes:
    Great tutorials you've made, thanks!
    I got one minor problem with the Pause Menu: when I pause the game, click to go to the main menu and start the game again from there (loading the first level), the game starts out paused.
    In the inspector the Pause Controller's Game Paused box isn't enabled though, and neither is the Pause Menu. Hitting Esc a few times (bringing up the menu and closing it) unpauses the game again. Any idea why this happens, or a way to get rid of it?

    ReplyDelete
    Replies
    1. I think I know what the problem is. Try making a start function that sets the gamePaused variable to false and sets the timeScale equal to 1. keep the controller enabled and the menu disabled and it should all work. My fault not yours ;) haha Thanks by the way. :)

      Delete
    2. Seems to work perfectly, thank you! :)

      Delete
  2. Hey,How Can i Make A Second Button?I Might Another Level Or Scene,Thanks.

    ReplyDelete
    Replies
    1. So like I said on YouTube, just repeat the same thing you did for the Start game button and quit button, but instead of Application.Loadlevel(1) do Application.Loadlevel(2) the variable inside the parenthesis will be the level number you are loading. Alternatively you can put a string inside the parenthesis like Application.LoadLevel("Level2"). That string would have to be what you saved the scene as in the editor.

      Delete
  3. Yhea I Had to create another var as second level and such.

    ReplyDelete
  4. how do i set a start function that starts the game off not paused

    ReplyDelete
    Replies
    1. In the start function put all the stuff that unpauses the game. Don't forget to put the boolean variable and set it to false.

      Delete
  5. Hey got one question, how do i stop my game while am on main menu?, i put the main menu script but i cant figure out how to stop my game while am on it

    ReplyDelete
    Replies
    1. I use the main menu in a different screen. if you're looking for a pause menu use the pause menu script :)

      Delete