[Solved] How to show text when in range of an object


Hi, I’m new to unity. I want to show text when the player is in range of wood block. “press E to pick the wood”. Everything else is setup, What i think i can do is keep throwing raycasts until i see the woodblock as collider but what i have read online that is not the way to go and is resource intensive. So I decided to go with box collider. But the problem is that the player stops when it hits the boundary of the box collider of wood. (I’m using default fps controller from standard unity assets). I don’t know how to get around this.


This box collider also doesn’t let the wood fall freely or let the player get inside this area. I want to make a separate collider that is used just to know if player is in range of the wood block and the other to not let player clip through it. Can this be done?

Having one raycast each frame is not ressource intensive. You can easily have one raycast at the location of the mouse or center of the screen (depending on the game), check for hits and do something. Eventually you’ll run into the problem that you have to check for wood, tree, door, … and possibly have different interaction-actions per item. To prevent this you could create an IInteractable interface and then have scripts like PickUpInteraction or OpenDoorInteraction implement this, and give these scripts to the corresponding prefabs / gameobjects. That way you can check if the item you hit with the raycast is an IInteractable type, and if so print some message and execute the Interact() method you wrote, which can have a different functionality per item.

Another way to do this, would be to add a trigger collider to the objects, which handles all of this. But i find the raycast solution more flexible, and it has no such problems as being able to stand in two large colliders at once.

1 Like

Thank you! Its just the answer i needed. I never noticed “is Trigger” checkbox. which fixed my problem in current case and i didn’t know about making intractable interfaces. But wouldn’t it be better to use trigger collider because i want to allow action when player is in range of an object and not aiming at the object (which would require me to send multiple ray-casts in multiple directions to do the same thing)?

You wouldn’t have to. You could have a trigger collider which surrounds the object so it can be larger and your raycast would hit that. That way, the player would at least be required to look in the general direction of the object. With a large area trigger collider, they could potentially be looking away from it and still interact.

1 Like

Thanks! i didn’t thought of that

Be aware tho, that these triggers should not be too large. This goes for bothapproaches. You run into problems if one trigger can be fully inside some other, or two triggers can overlap. This may cause the user the have a hard time interacting with the intended object.

1 Like