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 CalebJaggard · Sep 17, 2012 at 08:50 AM · 2dshooting

2D shooting problem please help!

Hi, i have created a simple Javascript that allows the player to shoot in the direction they are facing, with the left mouse. But i have a problem that i cant fix. I have also created a basic pause game script (JS also), but whenever i pause the game and click the GUI buttons (Like options etc), the game character fires the bullets while paused! (The projectiles remain static but build on each click). On resume, the bullets shoot in the direction i'm facing, depending on how many times i have clicked in the pause menu screen. Is there anyway to deactivate the shooting script when the escape key is press (When paused)?

Scripts used:

 var Bullet:Transform; 
 var shootForce:float;
 
 function Update() {
 
     if(Input.GetButtonDown("Fire1")) {
 
         var instanceBullet = Instantiate(Bullet, transform.position, Quaternion.identity);
         instanceBullet.rigidbody.AddForce(transform.forward * shootForce); 
     } 
 } 

And:

 var paused : boolean = false; 
 
 var myCheck : boolean = false;
 
 function Update () {
 
     if(Input.GetButtonDown("Pause")) {
 
         if(!paused){
 
             Time.timeScale = 0; paused = true;
         } else {
 
             Time.timeScale = 1;
             paused = false;
         } 
     } 
 }
 
 function OnGUI() {
 
     if(paused) {
 
         if(GUI.Button (Rect (10, 50, 100, 30), "Resume")) {
 
             Time.timeScale = 1.0f;
             paused = false;
         }
 
         if(GUI.Button (Rect (10, 90, 100, 30), "Options")) { 
             //Pause Game Options
         }
 
         if(GUI.Button (Rect (10, 130, 100, 30), "Exit")) { 
             Application.Quit(); 
         } 
     } 
 }
Comment
Add comment · Show 3
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 AlucardJay · Sep 20, 2012 at 02:05 PM 0
Share

I just formatted your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window =]

avatar image AlucardJay · Sep 20, 2012 at 02:10 PM 0
Share

There are 2 ways. Put your first script in a FixedUpdate() function.

Or : you already have a boolean, so use that. Change var paused : boolean = false; to

 public static var paused : boolean = false; 

then on the shoot script :

 if ( Input.GetButtonDown("Fire1") && NameOfPauseScriptHere.paused == false ) {
avatar image Shrandis · Dec 26, 2012 at 12:28 PM 0
Share

Seems like the OP has left this cute little question all alone in the sea of questions. What will happen to poor little question?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Shrandis · Sep 20, 2012 at 03:26 PM

Setting the timescale to 0 will not prevent your code from getting inputs and instantiating projectiles. You also need to check if the game is paused or not in your bullet shooting script before checking for mouse input and instantiating the projectile.

 void Update()
 {
 if (!paused)
     //check for mouse input and shoot bullet
 }

Hope it helps. Also, next time use the 101010 button to make your code easier to read.

Comment
Add comment · Show 3 · 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 AlucardJay · Sep 20, 2012 at 03:29 PM 0
Share

Hey there, that was my first comment too until I realized the inputs are on 2 different scripts. So this could work if the scripts were combined, but for one boolean variable to be shared, and being a direct reference to the timeScale, all scripts could benefit from a static variable isPaused.

avatar image Shrandis · Sep 20, 2012 at 04:38 PM 0
Share

I assumed the OP would know about passing variables between different scripts. But you're right, I should have written in detail. I'm new to unity answers so forgive me :)

avatar image AlucardJay · Sep 20, 2012 at 04:41 PM 0
Share

Hey, no worries, totally cool. And look - you receive the new user award for using the comment button ! I posted this for you from the moderator que, so the question was fresh in my $$anonymous$$d. Happy Coding =]

avatar image
0

Answer by lighting · Sep 20, 2012 at 03:30 PM

Can't you just check the area, where the mouse cursor is being placed and exclude the buttons' area ? I mean - sth like: if(Input.GetButtonDown("Fire1") && Input.mousePosition.y > yourValue && input.mousePosition.x > yourValue2) { ... }

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

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

13 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

Related Questions

Looking and firing towards mouse position, 2d game. 3 Answers

Top down 2d shooting - XZ rotation 0 Answers

Stop shooting infinite bullets with touch 3 Answers

hinge joint 2d is disconnecting/detaching/tearing 1 Answer

Transform.translate bullets problem 0 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