I’m trying to change my code to fire a raycast from a empty gameobject to the center of the screen. I’m very new to raycast so i am usure what is wrong with my code the problem becomes when i shoot the raycast from the camera to center of screen and when i zoom my camera back i cant use the object because the raycast is following the camera, how i can make that instead of the camera the raycast comes in a empty gameobject?
This is my current code :
var ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)); //i think is here the problem
if (Physics.Raycast(ray, out RaycastHit hit, interactDistance, LayerMask.GetMask("item")))
{
Interactable interactable = hit.transform.GetComponent<Interactable>();
if (interactable)
{
if (Interact)
{
anim.SetTrigger("interact");
interactable.Use(this);
}
}
}