GameObject GetObject(Vector2 pos)
{
Ray ray = Camera.main.ScreenPointToRay(pos);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
return hit.transform.gameObject;
}
return null;
}
I never had problems with raycasting before. This simple method always returns null even though there’s always an object at given Vector2 position. I have colliders attached to my objects, in fact I’m using raycasting in other scripts and they work just fine. So what could be the problem?
Edit : Well, I ended up using OverlapSphere instead but that used almost a quarter of my cpu since I had to call it every frame. So I’m back to simple raycast. Here’s more further info
Ray ray = new Ray(new Vector3(1,2,0), Vector3.zero);
print(ray.ToString());
What I get :
Origin : (243.4, 104, 0)...
How’s that even possible?