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 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 this 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

If one of these answers has resolved your concern, please vote it up and accept it. 2 Good answers here, you should at the very least, let everyone know if you have found what you were looking for.

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 this 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 within the Rect of the button. The answer on this question tells you how you can do this.

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

But isn't the GUI.button in its own "layer" ontop of the scene. The x,y cords are screen resolution and not the 3D plane x,y cords so how would I check if my click is in that rect?

avatar image chris_taylor · Jul 24, 2013 at 05:16 PM 0
Share

your pause button is in 3d space? If so you could raycast from the mouse pos and check if the ray intersects the bounds of the button

http://docs.unity3d.com/Documentation/ScriptReference/Bounds.IntersectRay.html

avatar image clunk47 · Jul 24, 2013 at 05:24 PM 2
Share

If your button is not GUI, and is in 3D space, just use a raycast like you're doing. Tag the button "PauseButton" for example. Then in your if(Physics.Raycast(ray, out hit)) statement, as if(hit.collider.tag == "PauseButton") and use that bool I talk about in my answer (canFire).

 Ray ray;
 RaycastHit hit;
 bool canFire;
 
 void Update()
 {
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
     if(Physics.Raycast(ray, out hit))
     {
         if(hit.collider.tag == "PauseButton")
             canFire = false;
         else
             canFire = true;
     }
 }

But if you're doing a "2D" shoot 'em up, why don't you just use Unity's simple GUI?

avatar image bliitzkriegx · Jul 24, 2013 at 05:31 PM 0
Share

I am using GUI.Button with a GUIS$$anonymous$$IN to texture/style it.

avatar image clunk47 · Jul 24, 2013 at 05:54 PM 1
Share

Our answers aren't quite the same. Yours is a reference to another question. He already knows how to use a button. He needs a condition in which he can or cannot fire.

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.

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

How do I replicate brushes selection in Unity terrain component? 0 Answers

Is it possible to detect which GUIStyleState is used? 2 Answers

Click on floor, record vector 3 position 2 Answers

GUI label on raycast? 0 Answers

Building placement validation on curved terrain 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