Detect if object is in an area

Hi.

I’m asking here because I lost some hours this week to find a solution to this problem.

I’m making a RTS/Tycoon like game, and scripting in C# (I’m a C# programmer). The camera is already set and I’ve already made the “instantiate the new building” part (he is already using OnCollisionEnter/Exit to allow/disallow placing an object inside another or things like that), and it’s working nicely.

But I just can’t figure out how to allow the player to put the object inside an specific area (and can’t find nothing that works, believe me, I’ve searched here, in Google, and can’t get a working answer or a light to this problem).

I’ve tried with OnTriggerEnter but haven’t got a nice result (because if any part of the object touches the trigger will change the object state, so half of the object will be inside the area, and half will be off, not working correctly). I just can’t figure out how to solve this problem.

Someone can give me a tip or something?

Thanks.

Can you make the assumption that your object has a specific bounding box shape? EX. this could be very easy if your objects are always approximately circles/spheres, just check if the center is one radius away from the bounds of the area collider.

It’s much more difficult with arbitrary colliders. On that post, at the bottom, the Raycasting idea sounds like it may work for any arbitrary shapes.