• 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 bliitzkriegx · Jul 24, 2013 at 05:02 PM · guiraycastterraingui-buttonclicking

GUI.button and onClick Terrain

I'm developing a 2D "Shoot'em up" game where my main character is stationary on the left of the screen and shoots where ever I click on the screen(using raycast). I have a pause button on the bottom right and every time I click the pause button, it shoots my main characters gun. Is there anyway to avoid t$$anonymous$$s happening as it looks messy and buggy.

Any help would be greatly appreciated. Thanks!

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 clunk47 · Aug 09, 2013 at 04:50 PM 1
Share

2 Replies

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

Answer by clunk47 · Jul 24, 2013 at 05:11 PM

Well if you define your pause button Rect in start or awake, you can setup a mouse position and check if that Rect contains it. Then you can have a bool that determine whether or not you are allowed to fire your gun. Since you didn't post your code, I'll give you a basic, easy to understand example. Hope t$$anonymous$$s helps :)

 Rect pauseBtnRect;
 Vector2 mouse;
 bool canFire;
 
 void Awake()
 {
     pauseBtnRect = new Rect(0, 0, 128, 64);
 }
 
 void Update()
 {
     mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
 
     if(pauseBtnRect.Contains(mouse))
         canFire = false;
     else
         canFire = true;
 
     if(canFire)
     {
         if(Input.GetButton("Fire1"))
         //Your firing code
     }
 
 }
 
 void OnGUI()
 {
     if(GUI.Button(pauseBtnRect, "Pause"))
         //Your pause code.
 }
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
1

Answer by chris_taylor · Jul 24, 2013 at 05:05 PM

When You test for the input for shooting the gun add a test to make sure the mouse is not wit$$anonymous$$n the Rect of the button. The answer on t$$anonymous$$s question tells you how you can do t$$anonymous$$s.

Comment
Add comment · Show 6 · 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 bliitzkriegx · Jul 24, 2013 at 05:08 PM 0
Share
avatar image chris_taylor · Jul 24, 2013 at 05:16 PM 0
Share
avatar image clunk47 · Jul 24, 2013 at 05:24 PM 2
Share
avatar image bliitzkriegx · Jul 24, 2013 at 05:31 PM 0
Share
avatar image clunk47 · Jul 24, 2013 at 05:54 PM 1
Share
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Close my GUI button by repressing the same Hot-key. 3 Answers

Use raycast to see if something else than terrain is under 1 Answer

help with errors 2 Answers

How to toggle the color of a button? 1 Answer

How can I add a distance cap to this script? 3 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