- Home /
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)
And while I posted it I might have found something. Will report in to tell if it works.
The method I was trying doesn't seem to work at all, the person in that thread has given wrong info. Next method then
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.
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); } } } } }
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
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.
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.
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.
Yep lol was fun figuring it out :D ... thanks for the upvote and accepted answer!
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.
The question was asked on Dec 31, 2011, after 7 years I am wondering how you could find this question. Nice work.
Your answer
Follow this Question
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