So i have 2D game and i wrote this piece of code hoping that it will work on 2d objects and it isn’t. Not returning any log, but if i add a 3d cube it returns an log. Can someone could help me transform this script to work in 2d gamescene?
Camera cam;
void Start()
{
cam = Camera.main;
}
void Update()
{
if (Input.GetMouseButtonDown(0)) //checks left mouse button
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition); //cast ray on whatever we click on
RaycastHit hit;
if (Physics.Raycast(ray, out hit))//if ray hits something, this happens
{
//destroy block in range
Debug.Log(" hit" + hit.collider.name);
}
}
}