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 KaiserJohan · Jan 31, 2012 at 11:43 AM · raycastinputmouse

Raycast based on a Rect?

Hello,

First I used Input.MousePosition to to select objects using raycasts. This works fine. However, now I have a crosshair which I render with a Texture2D, and I want the crosshair to be the origin of the raycast, like I previously did with Input.MousePosition.

How do I do this?

EDIT:

Heres what I use

 if (UseMouse)
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 else
     ray = Camera.main.ScreenPointToRay(new Vector3(Position.x + Position.width / 2, Position.y + Position.height / 2,0));
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

1 Reply

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

Answer by syclamoth · Jan 31, 2012 at 11:50 AM

Fixed screen coords:

If you have the rect that renders the crosshair, you can get the centre of it using

Vector2(rect.x + rect.width / 2, rect.y + rect.height / 2);

If you want to use this value in raycasting, you'll have to modify some things. ScreenPointToRay uses screen coordinates- these go from the bottom left to the top right as opposed to GUI coordinates, which go from top left to bottom right. I have no idea why they decided it would be a good idea to have inconsistencies like that, but there you go.

fixedScreenCoord = Vector2(rect.x + rect.width / 2, Screen.height - (rect.y + rect.height / 2));
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 KaiserJohan · Jan 31, 2012 at 12:02 PM 0
Share

Rect does not have a center attribute?

avatar image syclamoth · Jan 31, 2012 at 12:03 PM 0
Share

Hmm. So it doesn't. Sorry, I'm using the 3.5 beta- it does have one in that. In any case, it's just as simple.

avatar image KaiserJohan · Jan 31, 2012 at 12:09 PM 0
Share

It dosn't seem to raycast properly; i've updated my question with the code snippet. Thanks.

avatar image syclamoth · Jan 31, 2012 at 12:19 PM 0
Share

For the purposes of raycasting, you'll need to make some changes.

avatar image KaiserJohan · Jan 31, 2012 at 12:48 PM 0
Share

What do I need to change? At first looks, it appears the y-axis needs to be inverted somehow

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Mouse position giving errors. 0 Answers

Custom mouse cursor is laggy on low/mid hardware 0 Answers

Move player with mouse help 0 Answers

A node in a childnode? 1 Answer

Using RayCast to Get Mouse Input 1 Answer


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