Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by caleb_b · Mar 28, 2014 at 01:34 AM · inputplayer

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Josh707 · Mar 28, 2014 at 01:56 AM 0
Share

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
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shocking · Apr 01, 2014 at 09:22 PM 0
Share

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.
     }
avatar image caleb_b · Apr 02, 2014 at 08:54 PM 0
Share

$$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!

avatar image arkariarn · Apr 02, 2014 at 11:10 PM 0
Share
 Gameobject.Find("player").GetComponent<"PlayerController">();
avatar image caleb_b · Apr 07, 2014 at 02:42 AM 0
Share

Got it all figured out! Th anks all!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Stopping an object immediately 1 Answer

Ignore Player Input 4 Answers

Changing Action Map from Script doesn't update in the Inspector 0 Answers

Second Player Cannot Navigate the Menu 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges