Object Pickup range via script


I am working on an object-pickup-system. I already set up, that I can pick up an object when it’s collider was hit by a ray, when the player is within the PickupRange of 2 Meters from the object. That works fine. I used this code for this:

if (Physics.Raycast(ray, out hit, PickupRange))
        HittedObject = hit.collider.gameObject;
        CursorLock.canPickUp = true; 

But some object are really small and it’s hard to pick them up. So I would like to define, that the objects can be picked up not just then, when the collider was hit, but also, when the cursor is, lets say, about 30 cm from the object away.

I tried already solutions with additional colliders, but these didn’t work. So I’m searching for a solution via code.

What I found so far is just about the distance between the player an the object, but not about a range around an object itself.

What I am searching for is like:

When the Ray hits the collider or the defined range, then do bla bla.

Any ideas how I can reach that? In the picture I tried to explain, what I am trying to do. Hope this helps.

Thank you


Maybe for now, so you can test it:

  • add another Box Collider
  • make it a trigger
  • scale it to match the area like in the image

Now you can check for hit.collider.isTrigger that returns true or false. So:

if(hit.collider.isTrigger == true)

Anyway, this is with colliders, maybe you really want to explore it in code and in that case good luck :stuck_out_tongue: