Reputation: 1353
I have a simple pong game and when I pause the game by hitting the escape key, the user can still move the paddle left and right. The game is a mobile game that involves touch. How would I go about ensuring the paddle is not moveable when the game is paused as well?
Here is the code for my paddle if this helps:
private var ray : Ray;
private var hit : RaycastHit;
function Start () {
}
function Update () {
if(Input.GetMouseButton(0)){
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
transform.position.x = hit.point.x;
}
}
}
Here's my pause script:
var gamePaused : boolean = false;
var back : Texture2D;
var GUIskin:GUISkin;
var ClickSound:AudioClip;
function Start(){
Time.timeScale=1;
gamePaused = false;
gameObject.GetComponent(PauseMenu).enabled = false;
}
function OnGUI(){
GUI.skin = GUIskin;
GUI.Box (Rect (Screen.width - 550,Screen.height - 700,400,200), back);
if(GUI.Button(new Rect(Screen.width - 510,Screen.height - 615,120,80), "Main Menu")) {
Application.LoadLevel("Menu");
audio.PlayOneShot(ClickSound);
}
if(GUI.Button(new Rect(Screen.width - 310,Screen.height - 615,120,80), "Quit")) {
audio.PlayOneShot(ClickSound);
Application.Quit();
}
}
Also here's my pause controller:
private 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;
}
}
}
Upvotes: 1
Views: 3161
Reputation: 6548
Your paddle script is never told to check for your pause state.
In your update code you need to add a check here for some pause state:
function Update () {
if(!gamePaused){
if(Input.GetMouseButton(0)){
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
transform.position.x = hit.point.x;
}
}
}
}
Upvotes: 3
Reputation: 787
Just check to see whether the game is paused in your paddle's Update function, and if so, exit the function without moving the paddle. For example:
if(Time.timeScale == 0) return;
Upvotes: 0