- Home /
get mouse position on object
Hi, I'm a noob, so please be gentle...
I have a plane and want to convert the Screen Mouse Position to the position on that plane. (for some kind of strategy game) What's the best (most efficient) way to do this? Raycasting or using ScreenToWorldPoint? Either way I can't get it to work. Does anyone have some example code?
Thanks for your help!
Answer by skovacs1 · Sep 24, 2010 at 08:46 PM
You would likely want to raycast. You could use ScreenToWorldPoint and build your own ray or you could just use raycast with ScreenPointToRay to save yourself the effort.
How you choose to raycast is up to you. You can use a physics raycast which requires you have a collider on the object you are casting against and that it not be on the ignoreRaycast layer or you could raycast against the collider explicitly or you could raycast against the plane itself which is computationally somewhat cheaper, but only negligibly so.
//Using physics var hit : RaycastHit; if(Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit)) //we hit
//Raycast against a specific collider (plane is a gameObject or Transform) if(plane.collider.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit)) //we hit
//hit.Point would be the point in the world where the ray hit.
//Raycast against the plane itself (plane is a Plane) var enter : float; if(plane.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition))) //we hit
//enter gives us the distance along the ray, so //mainCamera.ScreenPointToRay(Input.mousePosition).getPoint(enter); //would give us the point in the world where the ray hit.
You say you want the point on the plane. For most purposes, having the world position is enough. If you actually need to know a point relative to the plane, then, assuming that your plane's transform pivot is properly centered and aligned, you would convert to object space and get the point relative to the plane.
//This is the position relative to the center of the plane.
//We should only need to care about the x and y coordinates as z should be 0.
var localPoint : Vector3 = plane.transform.InverseTransformPoint(hit.point);
Do as you like with that. If you need coordinates in some other system relative to the plane's scale or something, the calculation is straightforward.
Wow, thank you very much for the fast answer! I used the collider version and it works perfectly! I love the Unity community!
Answer by amuzulo · Mar 27, 2012 at 06:47 PM
I personally used this C# code based on the code from Scott Kovacs:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// Casts the ray and get the first game object hit
Physics.Raycast(ray, out hit);
Debug.Log("This hit at " + hit.point );
I already knew that there was a hit, so my code doesn't check if there is no hit. If there is no hit, this code returns (0.0, 0.0, 0.0).
If you want the name of the object hit just use:
Debug.Log("This hit at " + hit.transform.name );
This code has an error and is possibly deprecated because it returns (0.0,0.0,0.0) every time.
Your code has been placed in a void Update and the script has been attached to the $$anonymous$$ain Camera.
Answer by Neil Smith · Mar 21, 2014 at 10:21 AM
This give the true world position. Just create a Javascript file with this code in it and add it to the your main camera.
function Update(){
if(Input.GetMouseButtonDown(0)){
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
Debug.Log('Hit point: ' + hit.point);
}
}
}
Your answer
Follow this Question
Related Questions
How to make player's projectiles move to the mouse click position in a 3D top down shooter game? 0 Answers
Create GUI at mousePosition. 1 Answer
GUI Texture follow the mouse 1 Answer
How to get the mouse direction while left click is pressed? 1 Answer
Trigonometry: finding the vector3 positon of one corner using one side and one angle. 1 Answer