Raycast not hitting from close distance

I have 3D models on top of a table but if I am up at the table I can not pick up the objects. If I take a few steps back it works perfectly. Pretty new to raycasts so sorry if this is a easy answer or if I did something bad in my code.

public class PickUp : MonoBehaviour
{

    public int distance;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            Grab();
        }
    }

    void Grab()
    {
            RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
            Debug.DrawRay(ray.origin, ray.direction * distance, Color.red, 100, false);
            if (Physics.Raycast(ray, out hit, distance))
            {
                if (hit.transform.CompareTag("Item"))
                {
                    Debug.Log("Item Hit");
                GameObject ItemGrabbed = hit.transform.gameObject;
                    Destroy(hit.transform.gameObject);
                }

            }
        }
    }

If a raycast starts inside the collider, that collider won’t be hit. Is that possibly the issue here?

Honestly possible but I do not 100% know what that means im sorry. Fairly new to unity in general. could you by chance explain or link me to something that explains? I would appreciate it!

Nevermind I fixed it. My player was a capsule and the ray I believe was trying to go through it so made the capsule smaller and lifted camera up a bit and all good to go! Thanks for idea!

Just for your own clarification and future searchers, this is what I mean:
7006934--828632--raycast.png

Raycast A will detect the hit with the collider. Raycast B will not.

2 Likes

Another great debugging technique when using Raycast is to print the name of what you hit.

This information can come back to you via the RaycastHit or RaycastHit2D objects, in the form of the Collider or Collider2D involved.

You can use Debug.Log() to print its .name field and immediately see what else might be getting in your way.