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
1
Question by MichaelTaylor3d · Aug 21, 2012 at 08:40 PM · guirect

Detecting if mouse position is inside rect

This is supposed to be easy but for some reason its not working the way I think it should

I have a rect set up

 public static Rect BottomRegion = new Rect(Screen.height/2, Screen.width/2 + Screen.width/8,580,64);

and I want to detect if the mouse position is inside the rect so I created the following:

 if(BottomRegion.Contains(Input.mousePosition))
 {
 Debug.Log ("Inside");
 }

Pretty straight forward right?

Well when I detect my mouse position everything is backwards. The rect is located on the bottom of the screen, but the debug statement shows when the mouse is on the top of the screen.

the rect also defines where a gui box is located, and I can see the guiBox at the bottom of the screen to verify that the rect is set up right.

Comment
Add comment
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

2 Replies

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

Answer by MathieuBarbier · Aug 21, 2012 at 08:44 PM

y are inversed in those referentials. Use mousePosition.x and (Screen.Height-mousePosition.y).

   Vector2 mousePos = new Vector2(Input.mousePosition.x,Screen.height - Input.mousePosition.y); 

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 Garnagar · Oct 30, 2014 at 04:10 PM 0
Share

This doesn't seem to be working in Web Player. Does anyone know how to fix it there?

avatar image robertbu · Oct 30, 2014 at 04:26 PM 0
Share

@Garnager - you should open a new question and include the particulars. This answer is for a Rect in GUI coordinates, not Screen coordinates.

avatar image Garnagar · Oct 30, 2014 at 06:28 PM 0
Share

Ok, thanx for advice.

avatar image
1

Answer by Eric5h5 · Aug 21, 2012 at 08:47 PM

If you're talking about GUI coordinates, don't use Input.mousePosition. That's for screen coordinates. Use Event.current.mousePosition instead.

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 MichaelTaylor3d · Aug 21, 2012 at 09:07 PM 0
Share

Using Event.current.mousePosition creates a null referance exception. I tried the new keyword to no avail. $$anonymous$$aby Im implementing it wrong?

avatar image Eric5h5 · Aug 21, 2012 at 09:19 PM 0
Share

It needs to be used in OnGUI.

avatar image MichaelTaylor3d · Aug 21, 2012 at 09:23 PM 0
Share

Ok that makes sense, however this test isnt an OnGUI call, While im mapping a gui box to the rect, the rect is a separate static declaration.

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

9 People are following this question.

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

Related Questions

Convert world space to GUI Rect? 5 Answers

GuiTexture above 3D objects 1 Answer

Using a GUI.Button to Instantiate gameObject using another gameObject's location 1 Answer

Js Making windows/groups buttons react to other groups? 0 Answers

Buttons in Rects, opening other Rects? 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