Home > Software design >  Raycast from Camera in unity
Raycast from Camera in unity

Time:08-05

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");
        }
    }
}

CodePudding user response:

Use this:

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