Hello! I’m writing code for a SimCity-type game. I have classes for each building you can place, and a script that detects if collisions between buildings occur. If a collision occurs, it prevents the user from placing the building (because buildings can’t go on top of each other!).
I’m having trouble getting this collision script working though, could someone give me some advice on how to write the logic for this script and how to implement it?
To clarify, I’m not tryin to get someone to do the work for me >_> I already wrote the script and posted a question on UA, but wasn’t able to fix the code I had. The issues I was having are detailed there, but I’m looking to just start over from scratch with some fresh advice/guidance.
Thank you kindly!
basically you'll have an empty collider roaming around checking for collisions (you can resize colliders, just do a collider = spawnobject.collider for different buildings) but sinces its an empty game object and colliders are invisible its never seen
– sparkzbarcaHmmm, I like this idea in theory, but I think there might be a problem. When the user goes to build a building, the game object is instantiated at the mouse position, and follows the mouse around until the user clicks. So if I have the collision detector roams around with the mouse, once the user selects a building to build, it will constantly collide with the detector.
– murkantilismAlthough, I guess I could use layers to avoid this problem. Have "Built" and "Unbuilt" layers, with buildings being in the Unbuilt layer by default, and moved to Built once the user clicks, and the detector could check only in the Built layer. Thanks for the advice! I'll give this a try.
– murkantilismthe object shouldnt spawn initially with the collider on go into the prefab. turn the collider off. on mouse click spawn the building at the location and now since its actually in game turn it on. Problem solved :) mark as answerd
– sparkzbarcaNo, that still doesn't solve the problem. Only turning the collider on when a mouse click occurs will detect collisions for less than a second. Unless you meant turning the collider on permanently OnMouseClick, which would make me unable to place a second building unless I did an OnTriggerExit call. Which is a pretty convoluted way of doing it; I tried the solution you helped me come up with (using OnTriggerStay/Enter/Exit and checking which Layer the building is in). It took some debugging but I got it to work perfectly.
– murkantilism