- Home /
Collision between two instances of the same object
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!
Answer by sparkzbarca · Mar 23, 2013 at 09:35 AM
not too hard
create empty addcomponent collider istrigger = true
keep this object ALWAYS ON just move it around with the mouse its going to be a clone of the movemnt of the building when you spawn it and allow movement, except it always exists and always moves
canspawnstuff = true
ontriggerenter
canspawnstuff = false
ontriggerstay
canspawnstuff = false
ontriggerexit
canspawnnstuff = true
make sure you have the ontriggerstray you dont want to have 2 buildings inside your spawn radius
you move one out it makes it possible to spawn stuff but there is still an object int ehre this way you reset to false if another object is still in there
basically you'll have an empty collider roa$$anonymous$$g 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
Hmmm, 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.
Although, 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.
the 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
No, 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 On$$anonymous$$ouseClick, 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.