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 Paintbrush · Nov 12, 2012 at 06:14 AM · raycastmouseposition

Is there a way to get mouse position in 3D space at a given y-value?

I've got one solution to this problem by performing a raycast on a floor plane set at Y = 0 like so:

 RaycastHit hit;
 if(floor.Raycast(Camera.mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 100))
         {
             Vector3 hitPos = hit.point;
         }

but I'm wondering if there's a more efficient way of doing it without a raycast or a plane if I can specify Y as a constant? This may be the best solution out there, but I'm curious if anyone has a better one before I call it.

Thanks!

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 Zerot · Nov 12, 2012 at 11:41 AM

A slightly cheaper raycast would be against a Plane(assuming that currently floor is a collider).

Example:

 Plane plane = new Plane(Vector3.up,0);
 
 float dist;
 Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
 if (plane.Raycast(ray, out dist))
 {
   Vector3 point = ray.GetPoint(dist);
 }


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 Paintbrush · Nov 13, 2012 at 12:42 AM 0
Share

Edit: I was erroneously assu$$anonymous$$g you were suggesting creating a Plane collider in my previous comment... this is actually a good response, thanks!

avatar image $$anonymous$$ · Mar 26, 2013 at 10:08 AM 0
Share

if i do it like that i get an error: NullReferenceException UnityEngine.Camera.ScreenPointToRay (Vector3 position)

avatar image michael_house · May 25, 2014 at 02:51 PM 0
Share

If you get a null reference exception from running this code, it's because your camera doesn't have the tag 'Camera'.

avatar image
0

Answer by Seth-Bergman · Nov 12, 2012 at 06:42 AM

You could basically just drop the raycast:

Camera.main.ScreenToWorldPoint(Input.mousePosition)

but if you are using a downward-pointing camera, as it sounds, you will probably need to swap y and z coordinates.. since "z" is the distance from the camera, i.e.:

 var myTempPoint : Vector3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 var myWorldPoint : Vector3 = new Vector3(myTempPoint.x,myYConstant,myTempPoint.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 Paintbrush · Nov 12, 2012 at 07:32 AM 0
Share

Hi Seth,

thanks for the response. If I recall correctly, ScreenToWorldPoint would require me to pass in a Vector3 with the X and Y values set to screen space, and Z value set to distance of point of interest from camera. This would work well if I were using a downward-pointing camera since the Z would be constant, but I'm actually using a camera angled at 45 degrees to achieve an isometric 3rd-person feel. In order to set the distance of the point I'm interested in in the proper world space from that angle, I would ironically need to perform a raycast. Are my assumptions correct, or am I missing the gist of your solution?

Just to clarify, when I mentioned setting a constant Y, I meant in world space, not in camera local space.

p.s. I tried passing Input.mousePosition straight into ScreenToWorldPoint as suggested, but since Input.mousePosition's Z value (distance from camera) is 0, ScreenToWorldPoint will return the same coordinates regardless of where the mouse is on the camera - the camera's position in world space

avatar image Seth-Bergman · Nov 12, 2012 at 08:20 AM 0
Share

H$$anonymous$$$$anonymous$$, I was assu$$anonymous$$g an orthographic camera (among other wrong assumptions).. When you put it like that, I would say yes what you want is a Raycast!

avatar image Paintbrush · Nov 12, 2012 at 09:17 AM 0
Share

Sorry if my original inquiry was a bit ambiguous, and thanks for proposing a possible solution even after the fact. I'll keep my raycast implementation for now, until such time a better solution comes around, if it ever will.

What's the protocol for marking an answer correct? I'm new to using systems like this. Although the original answer didn't exactly solve my problem, I'd like to give you credit for trying and subsequently confir$$anonymous$$g my implementation.

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

12 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

Related Questions

Mouse Click + Raycast + Colliders 2 Answers

How to Limit Input.mousePosition or Raycast 1 Answer

Move toward Mouse Cursor (RaycastHit), without following? 1 Answer

Get Vector3 of Position Clicked on Plane 1 Answer

Click and Drag not Clicking or Dragging 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