- Home /
How can I make the player temporarily unable to move?
Hey guys. I have followed the Unity Space Shooter tutorial, adding my own touches and flairs once I had completed the project according to the tutorial. One of these changes was adding a larger space ship at the bottom of the screen that is destroyed if an asteroid hits it, causing a "game over" and prompting the player to press R to restart the game. My problem is that if the larger ship is destroyed, but the player's ship is not, the player can continue flying around, shooting the rest of the asteroids in the current wave. This allows the player to get extra points after the game has supposedly ended. I want to make the player unable to move if game is over. However, the player still needs to be able to press R to restart. So is there a way to block certain player input (arrow keys and WASD keys), but still register other inputs (R key)? Or is there a better way to go about this?
You can use the return keyword to end a method early, perhaps try something like checking the R key for input and then checking if the big ship is destroyed, if so then end the method there ins$$anonymous$$d of executing the rest (which is the movement).
void Update(){
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R)){
Restart();
}
if(big_ship.isDestroyed){
return;
}
//Input would then be below this
}
Answer by fifthknotch · Mar 28, 2014 at 02:06 AM
Add an if statements do a Boolean variable before any of the input portions of your code. It would look like this:
var canControl : boolean = true;
function Update () {
if (canControl) {
// Your input code
}
}
Then, when the bottom ship gets destroyed, just change canControl to false.
Answer by caleb_b · Apr 01, 2014 at 09:05 PM
First off, thanks a million to both of you for the answers. Now, both of your answers make sense. I'm just trying to figure out how to implement them. The problem is that I have two different scripts, one that manages the ships, and contains instructions for their destruction called DestroyByContact.cs. The other is called PlayerController.cs and contains the input code. I think the best way to go would be to take fifthknotch's solution, and make a bool variable. The DestroyByContact script would look something like this. Just for clarification, the bottom ship is tagged ProtectMe.
public bool canControl;
if(other.tag == "ProtectMe")
{
Instantiate(sSExplosion, other.transform.position, other.transform.rotation);
canControl = false;
gameController.GameOver ();
scoreValue = 0;
}
Then do something similar in the PlayerController script
public book canControl;
void FixedUpdate ()
{
if(canControl)
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
}
}
I have tried to add an if statement to the DestroyByContact script,
if(canControl = false
{
PlayerController.canControl = false.
}
but I got this error
Assets/Scripts/DestroyByContact.cs(43,42): error CS0120: An object reference is required to access non-static member `PlayerController.canControl'
I don't know if it's just that I am trying to access the canControl variable in the PlayerController script incorrectly?
You need to do a GetComponent to access the variables from another script.
PlayerController = pContoller;
void Start()
{
pController = GameObjectWithScript.GetComponent<"PlayerController">();
}
Then you can do:
if(canControl = false
{
pController.canControl = false.
}
$$anonymous$$akes sense... One thing though.
pController = GameObjectWtihScript.GetComponent<"PlayerController">();
The PlayerController script is attached to an object called Player. So I would substitute the word Player for your GameObejectWithScript? Sorry if its a dumb question, but I'm really new to all of this. I really appreciate the help!
Gameobject.Find("player").GetComponent<"PlayerController">();
Your answer