Raycast from Camera in unity

I try to shoot a raycast from the center of the Camera not from mouse position, how can improve my code for this?

if (Input.GetMouseButtonDown(0))
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, rango))
    {
        if (hit.transform.tag == "Player")
        {
            Debug.Log("ishitting");
        }
    }
}

>Solution :

Use this:

Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

Leave a Reply