- Home /
Non-Rigidbody equivalent to OnTriggerEnter?
Hello, world.
I am currently working on an RTS-style project to expand my knowledge of Unity and C#. For building placement, I already have my selected building following my mouse's position in three-dimensional space, set up a nice snap-to-grid feature, and can have buildings placed or canceled. However, I can not find a solution for the seemingly simple problem of preventing buildings from being placed in one another. I figured that I could simply try to detect if the bounds of the building I am going to place is intersecting the bounds of another building, but the only way I can find to do that is with OnTriggerEnter.
However, I don't want these buildings to be necessarily affected by physics, outside of being colliders that things such as units can not walk through. Is there another way to check if my building bounds intersect one another? Or is it necessary for me to use Rigidbodies? (And if so, can I detect the OnTriggerEnter for a child object? The building script is attached to an empty object, which has a child object that includes the mesh and collider.)
AndyMartin's idea of a temporary rigidbody seems to be the best. I have the rigidbody in the prefab, and I remove it upon placement. Now it works! Huzzah!
Rigidbody's with Is$$anonymous$$inematic set to true won't be affected by physics.
That seems like overkill doesn't it? I don't know what overhead a rigidbody adds by itself.
Answer by AndyMartin458 · Jun 23, 2014 at 08:04 PM
One object will need a rigidbody. Something cool that you could do is test the object that you are going to place by temporarily attaching a rigid-body to it. Have a script on all of the buildings with something like this:
//Place this on all the buildings
void OnTriggerEnter(collider other)
{
//there will need to be something
//that all of these objects are able
//to notify of a bad position
BuildingManager.BadLocation = true;
}
//making everything static to be easier for this demo
//Place this only on one manager object
public class BuildingManager : MonoBehaviour
{
private static bool badLocation
public static bool BadLocation
{
set{ badLocation = value; }
}
public static IEnumerator TestBuildingLocation()
{
//... instantiate some kind of prefab
badLocation = false;
building.AddComponent<RigidBody>();
//... this will need to wait so that the other buildings have time to register the collision
yield return WaitForFixedUpdate();
if(badLocation == false)
Destroy(building.GetComponenent<RigidBody>()
else
Destroy(building);
}
}
Answer by Jeff-Kesselman · Jun 23, 2014 at 07:47 PM
Any moving object needs a RigidBody in order to have a collider. This is because RigidBody is responsible for knowing about motion and when colliders need to be checked. Its actually a lot LESS overhead then if you just had a Collider you were checking every frame.
If you set "isKinematic" then you wont be effected by physics forces. If you set the collider as isTrigger then its movement wont be stopped by other objects.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnTriggerEnter On seperate object (than the script is attached to) 1 Answer
how do i instantiate game objects in an rts game 2 Answers
How to apply A* algorithm in C#? 2 Answers