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 /
This question was closed Jul 02, 2017 at 09:18 AM by hexagonius for the following reason:

The question is answered, right answer was accepted

avatar image
8
Question by Toomuchbob · Jan 08, 2013 at 01:42 PM · screenpointtoray

Get world coordinates from mouse click

Questions like this have been asked before but not quite in the context im looking for. I need to find the world coordinates of a point that is clicked on a screen. I can only get this working when clicking on a gameObject, but I want it to find the coordinates whether or not there is.anything to click there.

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 SleazyDutcham · Oct 25, 2016 at 02:54 AM 0
Share

The answers to this are assu$$anonymous$$g a 3D project, but the question makes a lot more sense in a 2D context.

avatar image SleazyDutcham · Oct 25, 2016 at 03:01 AM 0
Share

Vector2 clickLocation = Camera.main.ScreenToWorldPoint(Input.mousePosition); works well for this in 2D space

2 Replies

  • Sort: 
avatar image
15
Best Answer

Answer by AlucardJay · Jan 08, 2013 at 01:50 PM

As you have probably read, using a raycast will return the hit.point in the world-space. If you are not clicking on a collider, then how far away from the camera should your input return a world-space position. It would be like pointing at the sky and saying "put a star there", where? how far away?

If you don't have a collider to raycast against for depth reference, but you know the distance from the camera you would like the point to be, you can use Ray :

 #pragma strict
 
 public var distance : float = 4.5;
 
 function Update() {
     CastRayToWorld();
 }
 
 function CastRayToWorld() {
     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);    
     var point : Vector3 = ray.origin + (ray.direction * distance);    
     Debug.Log( "World point " + point );
 }
Comment
Add comment · Show 5 · 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 Huminaboz · May 13, 2013 at 01:13 AM 0
Share

This helped a ton, thanks!

avatar image mhhk88 · Oct 22, 2013 at 08:47 AM 0
Share

Best answer I've been lookin for...

avatar image robertbu · Oct 22, 2013 at 06:26 PM 0
Share

FYI: The ray created by Camera.ScreePointToRay() starts at the near clip plane, not at the camera position, so you need to subtract that from the 'distance' if 'distance' is critical. Camera.ScreenToWorldPoint() is another way to make this conversion.

avatar image mhhk88 · Oct 23, 2013 at 01:25 AM 0
Share

I see. You gave me one step deeper insight. Thanks. Anyway, this is so confusing matter....

avatar image Blue-Studios · Jul 02, 2017 at 08:58 AM 0
Share

@alucardj How do you get the coordinates in variables

avatar image
2

Answer by whydoidoit · Jan 08, 2013 at 01:48 PM

There is no necessary coordinate of a position clicked on the screen - you have to click something to give you a perspective and distance. Normally what you would have is a very large plane representing the ground and see where on that you clicked.

Comment
Add comment · Show 2 · 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 AlucardJay · Jan 08, 2013 at 02:21 PM 0
Share

Nice response, that is what I was trying to portray with my point to the sky analogy =]

avatar image whydoidoit · Jan 09, 2013 at 05:31 AM 0
Share

Yours is certainly more poetic!

Follow this Question

Answers Answers and Comments

15 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

Related Questions

ScreenPointToRay gives unexpected results? 2 Answers

Variable is assigned but never used for ScreenPointToRay 1 Answer

ScreenToRayPoint not working in VR, how to substitute? 0 Answers

Rotation while movement problem 3 Answers

Add Force based on Raycast 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