// Use this for initialization void Start() { Time.timeScale = 1.0f; //Set this to 1 so the game plays in realtime state = Shots_Remaining.FIVE_REMAINING; //The player states of having 5 shots height = Height_Selected.LOW; // The default selected height is LOW audioSource = GetComponent <AudioSource>(); thePlayer = GameObject.Find("First Person Controller"); hSliderValue = 0.0f; // The power bar slider starts off at 0.0 initially target = maxSliderValue; value = 1.0f; //THis is the value at which the power bar will increase shotsRemaining = 5; ball.transform.position = GameObject.Find("BallSpawnPoint" + numberOfShotsTaken).transform.position; //set the balls position activeHeight = 1; }
// Update is called once per frame void Update() { /* * If this bool is true, it means that the power of teh shot is at least 9.8/10. When this is the case, the * shot is taken in slow motion for added effect. */ if (slowMotion) { Time.timeScale = 0.4f; } if (!slowMotion) { Time.timeScale = 1.0f; } /*The distance between the player and the ball. If the player is close enough to the ball, they * can kick it. The PerformShot() functions is called. */ distance = Vector3.Distance(thePlayer.transform.position, ball.transform.position); if (Input.GetMouseButtonUp(0) || Input.GetButtonUp("xbox_a")) { if (distance <= 5) { PerformShot(); } } /* * Pressing M toggles the sound on and off in the game. So does pressing x on xbox controller */ if (Input.GetKeyUp(KeyCode.M)) { AudioListener.pause = !AudioListener.pause; } /* * This displays and hides the objective gui that is displayed in the bottom left hand corner of the screen */ if (Input.GetKeyUp(KeyCode.B)) { objective.enabled = !objective.enabled; } /* * If the player presses Escape, it brings them to the main menu */ if (Input.GetKeyUp(KeyCode.Escape)) { Application.LoadLevel("MainMenu"); } /* * If the player pressess I, it displays the Instructions. Can also press x on the xbox controller */ if (Input.GetKeyUp(KeyCode.I) || Input.GetButtonUp("xbox_x")) { InstructionsDisplayed = !InstructionsDisplayed; } /* * Pressing P or pressing the Start button on the xbox controller pasuses the game. I discovered that if you * have an interactive cloth in your game when you pause, it will cause the game to mess up and run at less * than 1 frame per second, so I use 2 methods I created called DisableAllCloths() and EnableAllCloths() to * disable and enable clothes while pausing and unpausing. */ if (Input.GetKeyDown(KeyCode.P) || Input.GetButtonDown("xbox_pause")) { gamePaused = !gamePaused; } if (gamePaused) { DisableAllCloths(); } /* * Here I make sure the game is not running in slow motion before I call EnableAllCloths() as this function * also affects the speed at which the game is run */ if (!gamePaused && !slowMotion) { EnableAllCloths(); } /*This is the bool that toggles whether the instructions are displayed or not */ if (InstructionsDisplayed) { instructions.guiText.enabled = true; } if (!InstructionsDisplayed) { instructions.guiText.enabled = false; } /*Once the player has taken all their shots, I call the EvaluatePlayer() method to see how they did. The * EvaluatePlayer() method will determine whether they have passed the level or not. */ if (shotsRemaining <= 0) { EvaluatePlayer(); } /*If the ball has been kicked, I start a timer that lasts for 4 seconds. This gives the ball enough time to travel * towards the goal. Then, once the ball has stopped rolling, it is reset to the next position where the player takes * their next shot from */ CheckIsBallKicked(); /*Depending on how many shots the player has taken, I change the state. */ if (numberOfShotsTaken == 0) { state = Shots_Remaining.FIVE_REMAINING; } else if (numberOfShotsTaken == 1) { state = Shots_Remaining.FOUR_REMAINING; } else if (numberOfShotsTaken == 2) { state = Shots_Remaining.THREE_REMAINING; } else if (numberOfShotsTaken == 3) { state = Shots_Remaining.TWO_REMAINING; } else if (numberOfShotsTaken == 4) { state = Shots_Remaining.ONE_REMAINING; } else if (numberOfShotsTaken == 5) { state = Shots_Remaining.NONE_REMAINING; } /*If the ball somehow falls throw the ground and ends up out of the stadium, once it falls below the stadium, * NextShot() will be called, and the ball will be positioned ready for the next shot to be taken. */ if (ball.transform.position.y <= -5) { NextShot(); } /*This calls the PowerBar() function. This function moves the power slider from its minimum value to the maximum * value and vise versa */ PowerBar(); //right click to select height if (Input.GetMouseButtonUp(1) || Input.GetButtonUp("xbox_b")) { activeHeight += 1; } if (activeHeight > 3) { activeHeight = 1; } if (activeHeight == 1) { height = Height_Selected.LOW; } if (activeHeight == 2) { height = Height_Selected.MEDIUM; } if (activeHeight == 3) { height = Height_Selected.HIGH; } /*This switch statement lets me control the height that is selected by the player. If the selected height is * HIGH, then the MEDIUM, and LOW options are grayed out so it is clear which has been selected. THis is done * for the LOW and MEDIUM cases too. I also store the height of the shot so it can be used in * the PerformShot() function when the shot is taken. */ switch (height) { case Height_Selected.HIGH: heightNotSelected = low.color; heightNotSelected = new Color(47, 47, 47); heightNotSelected.a = 0.1f; low.color = heightNotSelected; heightNotSelected = medium.color; heightNotSelected = new Color(47, 47, 47); heightNotSelected.a = 0.1f; medium.color = heightNotSelected; heightNotSelected = high.color; heightNotSelected = new Color(128, 128, 128); heightNotSelected.a = 1.0f; high.color = heightNotSelected; shotHeight = 2.0f; break; case Height_Selected.MEDIUM: heightNotSelected = low.color; heightNotSelected = new Color(47, 47, 47); heightNotSelected.a = 0.1f; low.color = heightNotSelected; heightNotSelected = high.color; heightNotSelected = new Color(47, 47, 47); heightNotSelected.a = 0.1f; high.color = heightNotSelected; heightNotSelected = medium.color; heightNotSelected = new Color(128, 128, 128); heightNotSelected.a = 1.0f; medium.color = heightNotSelected; shotHeight = 1.0f; break; case Height_Selected.LOW: heightNotSelected = high.color; heightNotSelected = new Color(47, 47, 47); heightNotSelected.a = 0.1f; high.color = heightNotSelected; heightNotSelected = medium.color; heightNotSelected = new Color(47, 47, 47); heightNotSelected.a = 0.1f; medium.color = heightNotSelected; heightNotSelected = low.color; heightNotSelected = new Color(128, 128, 128); heightNotSelected.a = 1.0f; low.color = heightNotSelected; shotHeight = 0.2f; break; } /*This switch statement allows me to control the GUITextures in the bottom right hand corner of the screen. * These GUI balls represent the amount of shots remaining. When the player takes a shot, I grey out a ball * so it is clear to them how many shots they have left. */ switch (state) { case Shots_Remaining.FIVE_REMAINING: //Everything is automatically enabled on START() for this state so I neeto do nothing else break; case Shots_Remaining.FOUR_REMAINING: shotTakenColor = shotsRemaining5.color; shotTakenColor = new Color(47, 47, 47); shotTakenColor.a = 0.1f; shotsRemaining5.color = shotTakenColor; break; case Shots_Remaining.THREE_REMAINING: shotTakenColor = shotsRemaining5.color; shotTakenColor = new Color(47, 47, 47); shotTakenColor.a = 0.1f; shotsRemaining4.color = shotTakenColor; break; case Shots_Remaining.TWO_REMAINING: shotTakenColor = shotsRemaining5.color; shotTakenColor = new Color(47, 47, 47); shotTakenColor.a = 0.1f; shotsRemaining3.color = shotTakenColor; break; case Shots_Remaining.ONE_REMAINING: shotTakenColor = shotsRemaining5.color; shotTakenColor = new Color(47, 47, 47); shotTakenColor.a = 0.1f; shotsRemaining2.color = shotTakenColor; break; case Shots_Remaining.NONE_REMAINING: shotTakenColor = shotsRemaining5.color; shotTakenColor = new Color(47, 47, 47); shotTakenColor.a = 0.1f; shotsRemaining1.color = shotTakenColor; break; } }