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 xxluky · Jan 28, 2015 at 10:26 AM · 2dguiraycast

How to exclude OnGUI button from RayCastHit?

Hi guys, I control a player by touching the screen using Ray. I also have a button for firing named "Cube". I excluded this cube from the ray like this:

 if (rayCastHit.transform.gameObject.name != "Cube")

It causes that I can fire without moving the player. Now I want the same with a GUI button ("Paused"/"Resume" - two switching buttons on the same position). How can I exclude this buttons so it is out of the ray? Whitch condition do I have to add?

Thanks

Comment
Add comment · Show 2
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 gheeler · Jan 29, 2015 at 09:46 AM 0
Share

Are you saying that you don't want to raycast when you click a button?

avatar image xxluky · Jan 29, 2015 at 09:53 AM 0
Share

Yes, exactly... How would you do it?

2 Replies

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

Answer by hav_ngs_ru · Jan 28, 2015 at 10:38 AM

    if (rayCastHit.transform.gameObject.name != "Cube"  
        &&  rayCastHit.transform.gameObject.name != "Paused"  
        &&  rayCastHit.transform.gameObject.name != "Resume") {
      ...
    }




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 xxluky · Jan 28, 2015 at 10:53 AM 0
Share

No luck. The GUI is not a game object. It is just script. I add this button to the screen like this:

 transform.gameObject.AddComponent<PauseButton>();

I enclose the whole script:

 using UnityEngine;
 using System.Collections;
 
 public class PauseButton : $$anonymous$$onoBehaviour {
     
     public string buttonToken = "Resumed";
     public const string pausedToken = "Paused";
     public const string ResumedToken = "Resumed";
     
     void OnGUI()
     {
         const int buttonWidth = 90;
         const int buttonHeight = 25;
 
         switch (buttonToken)
         {
         case ResumedToken:
         {
             if (GUI.Button (new Rect (Screen.width - (buttonWidth + 30), Screen.height / 10 - buttonHeight,
                                       buttonWidth, buttonHeight), "Pause"))
             {
                 buttonToken = pausedToken;
                 Time.timeScale = 0f;
             } 
             break;
         }
         case pausedToken:
         {
             if (GUI.Button (new Rect (Screen.width - (buttonWidth + 30), Screen.height / 10 - buttonHeight,
                                       buttonWidth, buttonHeight), "Resume"))
             {
                 buttonToken = ResumedToken;
                 Time.timeScale = 1f;
             }
             break;
         }
         default:
         {
             break;
         }
         }
     }
 }

avatar image hav_ngs_ru · Jan 29, 2015 at 09:41 AM 0
Share

In this case you could just read buttons condition and dont raycast if some of them pressed.

avatar image xxluky · Jan 29, 2015 at 09:43 AM 0
Share

Hi, thank you for your advice. How could I do it, in the script?

avatar image hav_ngs_ru · Jan 30, 2015 at 03:50 PM 0
Share

I wouldnt write all code for you, but solution for detecting "click" and "hold" conditions is wrote here for example: http://answers.unity3d.com/questions/861338/guirepeatbutton-returns-true-false-true-false.html

the @AdamScura's answer contains more applicable code example for your case.

avatar image
0

Answer by gheeler · Jan 29, 2015 at 10:05 AM

 public class GUIManager : MonoBehaviour{
     public static bool InputOverGUI (Rect[] guiRects) {
         inputPos = new Vector2[Input.touchCount];
         for (int i=0; i<Input.touchCount; i++) {
             inputPos[i] = Input.touches[i].position;
         }
         foreach (Vector2 v in inputPos) {
             foreach (Rect rect in guiRects) {
                 if (rect.Contains(v))
                     return true;
             }
         }
         return false;
     }
 }

This function will check each touch and if any touches are in any guiRects it will return true otherwise it will return false.

This isn't tested so you may need to debug... You may have to invert the y of the input i.e.

  inputPos[0].y = Screen.height-inputPos[0].y

Comment
Add comment · Show 1 · 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 xxluky · Jan 29, 2015 at 10:32 AM 0
Share

Well, I tried, but I am just a beginner and your code is too complex to me for now. I thought only to add another condition will do the trick... Sorry.

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

21 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

Related Questions

GUI Follow RaycastHit 2 Answers

Making raycasts go to the centre of the screen 1 Answer

Issue with raycasting in 2D 1 Answer

How to render a colored 2D rectangle. 6 Answers

Unity Physics2D.Raycast always returns “the invoker” as hit 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