- Home /
Question by
kaisean.development · Mar 20, 2012 at 06:49 PM ·
objectmouseraycastingmousepositionpixel
How to find out which object is under a specific pixel
As simple as it gets:
How to find out which object is under a specific pixel?
For example, something like this:
GameObject ObjectUnderPixel(float x, float y)
{
// algorithm here
}
I'm guessing it has to do with raycasting, but I am not sure how to position the ray.
Thanks
Comment
Best Answer
Answer by DaveA · Mar 20, 2012 at 07:09 PM
Assuming by 'pixel' you mean a screen coordinate, then the code is mainly here: http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html?from=Input Note the object must have Collider on it
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
// Debug.DrawLine (ray.origin, hit.point);
var objectHit = hit.collider.gameObject;
}
Your answer
Follow this Question
Related Questions
Ray Over Mouse 1 Answer
How to Limit Input.mousePosition or Raycast 1 Answer
Raycast2d not working C# :( 1 Answer
Dragging movement Speed 1 Answer