Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Simon V · Dec 31, 2011 at 12:03 PM · mouseplanecoordinates

Get mouse positon in world coordinates on a plane?

I've been trying the methods described here: Link. But this is giving me a lot of errors. I can't retrieve the objects I need for these functions. The exact method described there gives me errors saying that the objects don't exist.

If I then do GameObject.FindGameObjectWithTag() I will get errors saying that GameObject.FindGameObjectWithTag does not have the functions described in my link.

If I do a GetComponent<>, it can't find the objects.

Any idea what I could try?

EDIT: The method that seems to be giving the least errors is:

 if (plane.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),enter))
         {
             drawingPosition = mainCamera.ScreenPointToRay(Input.mousePosition).GetPoint(enter);
         }

But I can't figure out what I should fill in in the "out float enter" parameter. The documentation isn't particularly helpful either; it doesn't even mention that parameter (Link)

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 Simon V · Dec 31, 2011 at 12:04 PM 0
Share

And while I posted it I might have found something. Will report in to tell if it works.

avatar image Simon V · Dec 31, 2011 at 12:07 PM 0
Share

The method I was trying doesn't seem to work at all, the person in that thread has given wrong info. Next method then

avatar image Simon V · Dec 31, 2011 at 12:09 PM 0
Share

The second method I tried seems to have the wrong info as well. The first one, I can't try because I need to raycast against a plane which might be covered: so I might be hitting another object. And the fourth method is no option either: it gives the local coordinates.

2 Replies

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

Answer by Lo0NuhtiK · Dec 31, 2011 at 01:03 PM

See if this is what you're looking to do -->

var rodentButton : int = 0 ; //set to left mouse button with '0', can change in inspector
var rodentRange : float = 250.0 ; //set in inspector how far into 3d space from the screen you can click
var tagger : String = "MyPlaneTag" ; //set this in inspector to whatever you tag your plane/whatever
function Update(){
   if(Input.GetMouseButtonDown(rodentButton)){
      FindTheCheese() ;
   }
}
 
function FindTheCheese(){
   var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition) ; //make sure you have a camera in the scene tagged as 'MainCamera'
   var hit : RaycastHit ;
      
   if(Physics.Raycast(ray, hit, rodentRange)){
      if(hit.transform.CompareTag(tagger)){
         //do whatever you're wanting to do here...
      Debug.Log("World coordinates of our mouse click = " +hit.point) ; //a debug so you can see what it's returning. "hit.point" here is what returns the coordinates
      }
   }
}


Edit

I'm not too sure about C# or splitting up functions in it, but here's my best guess at translating the above and just putting it all into the update() ->

using UnityEngine; using System.Collections;

public class NameItWhatever : MonoBehaviour {   public int rodentButton = 0; public float rodentRange = 250.0f; public string tagger = "MyPlaneTag";   void Update(){ if(Input.GetMouseButtonDown(rodentButton)){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray, out hit, rodentRange)){ if(hit.transform.CompareTag(tagger)){ //do whatever...... Debug.Log(hit.point); } } } } }

Comment
Add comment · Show 18 · 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 Simon V · Dec 31, 2011 at 01:20 PM 0
Share

Thanks for the answer, but it gives me three errors. error CS0165: Use of unassigned local variable hit'`, error CS1502: The best overloaded method match for UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit, float)' has some invalid arguments and error CS1620: Argument #2' is missing out' modifier`. I do have to note that I'm using C# and not JavaScript

avatar image Lo0NuhtiK · Dec 31, 2011 at 01:22 PM 0
Share

That would be why all the errors lol ... the js in a C# file
I just noticed your other comment about the plane may be hidden behind other things also, so this wouldn't exactly work because it would only return the coord's if you could click on the object ; it would need modified a bit.

avatar image Simon V · Dec 31, 2011 at 01:26 PM 0
Share

I did change it to its C# equivalent, don't worry ;) And actually, now that I think about it: It might not matter if objects are in front of it. I only need the x and z coordinate, not the y coordinate.

avatar image Lo0NuhtiK · Dec 31, 2011 at 01:34 PM 0
Share

Oh, ok lol ... I just edited my post and added what I think C# would have also, not sure. Didn't see your last post til now.
EDIT : and as for splitting the coord's, I'm thinking
"hit.point.x" or "hit.point.z" would probably work.

avatar image Lo0NuhtiK · Dec 31, 2011 at 02:07 PM 1
Share

Yep lol was fun figuring it out :D ... thanks for the upvote and accepted answer!

Show more comments
avatar image
1

Answer by barthdamon · Nov 30, 2018 at 02:38 AM

If you know the equation of the plane you can do something like this:

         Vector3 planeNormal = new Vector3(0f, 1f, 0f);
         Vector3 planeCenter = plane.transform.position;
         Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         Vector3 lineOrigin = cameraRay.origin;
         Vector3 lineDirection = cameraRay.direction;
 
         Vector3 difference = planeCenter - lineOrigin;
         float denominator = Vector3.Dot(lineDirection, planeNormal);
         float t = Vector3.Dot(difference, planeNormal) / denominator;
 
         // INTERSECTION:
         Vector3 planeIntersection = lineOrigin + (lineDirection * t);


I found this helpful: https://courses.cs.washington.edu/courses/csep557/10au/lectures/triangle_intersection.pdf

Intuitively you put the equation of your ray into the plane equation to solve for the distance from the origin the line intersects the plane, then plug that back into the ray equation to find the point on the plane.

Comment
Add comment · Show 1 · 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 Nazirzadeh · Nov 30, 2018 at 02:44 AM 1
Share

The question was asked on Dec 31, 2011, after 7 years I am wondering how you could find this question. Nice work.

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

8 People are following this question.

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

Related Questions

How to get mouse position in World Space when in Editor Mode 1 Answer

How do I create a shape that rotates from one pivot point, to always point towards the mouse cursor? 2 Answers

Moving object on plane 1 Answer

Event System value of mouse position is wrong. 0 Answers

Find Mouse co-ordinates on click in a 2d environment 5 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