Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Loftless · Jun 23, 2011 at 07:32 PM · cameraraycastscreentoworldpoint

Camera.main.ScreenToWorldPoint not outputting expected results.

Hey, this one has been stumping me for a few days now, and I keep avoiding it.

So, I'm trying to do a simple Raycast from mouse position to the world. I'm using: Physics.Raycast( Camera.main.ScreenToWorldPoint(Input.mousePosition), Camera.main.transform.TransformDirection(Vector3.forward), mouseHitInfo ); //Cast down

Which didn't seem to be giving me the results I was expecting, so I logged the output, and Camera.main.ScreenToWorldPoint is giving me the same position every single time. (10, 12, 3).

As a note I'm using a camera facing down, on a 30 degree angle. I am calculating that angle in the code above with TransformDirection, so I don't see the issue...

Any help on the matter is much appreciated, and thanks in advance.

-Rob

Comment
Add comment · Show 3
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 flaviusxvii · Jun 23, 2011 at 08:11 PM 1
Share

Try this example... http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html

avatar image Loftless · Jun 24, 2011 at 12:25 AM 0
Share

Wow, you're a life saver. No clue to use ScreenPointToRay, thanks :)

avatar image Eric5h5 · Jun 24, 2011 at 12:45 AM 0
Share

Ins$$anonymous$$d of transform.TransformDirection(Vector3.forward), you can just do transform.forward.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Waz · Jun 24, 2011 at 12:32 AM

The reason your code doesn't work is that Input.mousePosition has a zero Z co-ordinate - i.e. 0 units from the camera, which I'm guessing was at (10,12,3). ScreenPointToRay ignores the Z and produces a Ray from the camera through the screenpoint, which you can then use as a raycast. To understand this, note that the Ray you get from ScreenPointToRay always has its origin at the Camera (it will be (10,12,3) in your case, since that is the line from your eye (the camera) through the mouse position, into the world.

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

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 on terrain 1 Answer

Camera.ScreenToWorldPoint with Perspective camera 1 Answer

Raycast hitting objects to the left of my player 1 Answer

Ray Cast not working for camera. 1 Answer

ScreenToWorldPoint has some sort of offset 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