• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Kowbell · Jun 23, 2014 at 07:18 PM · c#ontriggerenterrts

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!

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image karlhulme · Jun 23, 2014 at 07:31 PM 0
Share

Rigidbody's with IsKinematic 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.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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);
     }
 }
 
 





Comment
Kowbell

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to Lerp several objects positions with one trigger event? 0 Answers

How to Change a variable in another script when ontriggerenter 0 Answers

OnTriggerEnter 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges