Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by menep · May 15, 2012 at 08:44 AM · collisioncolliderrts

Checking "if collision" from a script?

I try to write an RTS game. For placing buildings I need to check if they collide with other buildings.

I tried to use: OnTriggerEnter, OnCollisionEnter ...with different sets of an GameObject like: isTrigger, isKinematic, RigidBody...

When I had meshCollider and rigidbody the buildings on collision where just jumping from each other, or they just slope down from a hill.

The OnTriggerEnter and other events are checked when Im out of a method and there is one loop in Update(). And they dont return much.

The best way for me would be to have a method that returns true or false on collision. I would like to check it after I place the building too close to other object. Objects are in different sizes and shapes, so I dont know if checking some kind of radius will help.

Comment
Add comment · Show 4
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 menep · May 16, 2012 at 10:56 AM 0
Share

I dont know why Ive got some problems. There is a nice table "Collision action matrix" taht shows different combinations. But I cannot run OnTriggerEnter, OnCollisionEnter, OnTriggerStay, OnCollisionStay if the object has no rigidbody or it is$$anonymous$$inematic.

I tried for me the best: to have one building with rigidbody , and without mesh collider to collide with already egzisted one which has mesh collider and without rigidbody. According to the table they shoud gibe me "Collision detection occurs and messages are sent upon collision".

Should I have also mesh collider to the moving building? It would complicate my code, because now the placed building is moving where is the mouse cursor. If I add collider to it at this stage it will block the ray to the terrain.

avatar image Bunny83 · May 16, 2012 at 11:03 AM 0
Share

Like @$$anonymous$$iraSensei said: colliders can only collide with other colliders / triggers. Also the moving object should have a rigidbody attached. I'm still confused about your setup. Can you clearly explain what objects you have, what object should collide with what and do they have colliders attached.

Also note that a meshcollider can't collide with other meshcolliders unless it is a convex meshcollider. $$anonymous$$eshcolliders are quite heavy to compute so basically you would use them on static objects. $$anonymous$$oving objects are better off with primitive colliders (box, sphere, capsule)

avatar image menep · May 16, 2012 at 11:53 AM 0
Share

$$anonymous$$y problem is solved. But its not related to the topic. "If collision" from a script was necessary.

I deleted if(hit.transform.tag == "Terrain") and I could use collider in the gameObject before placing it :-). I find OnTrigger... better becuse I can add reference to the game object by other.gameObject in void OnTriggerStay(Collider other). I still dont know how to find reference to my gameObject in OnCollisionEnter(Collision collision). Im not using OnCollision any more but it would be nice to know how find collided gameObject?

Thanks again

avatar image Bunny83 · May 17, 2012 at 01:53 AM 0
Share

@menep: comments! Use comments if you don't answer the question.

OnCollisionEnter does actually give you a lot more information than just what object you collided with. Take a look at the Collision struct. collision.gameObject gives you the reference of the collided object. You get also all contact points. Each contact point gives you the exact point and collision normal vector which is very useful in many cases.

However collisions affect the objects movement since they are blocked by the physicssystem (penalty forces get applied when two objects intersect). Triggers are useful when you just want to detect that two objects touch / intersect. Triggers have no influence on the movement, they are "non solid".

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by KiraSensei · May 15, 2012 at 11:08 AM

If i'm not mistaking, to correctly use OnTriggerEnter you need colliders on both objects, a rigidbody on one of them only - very important - (the one moving for example..), and one of the isTrigger activated (the one attaching the script).

I actually had some issues with mesh collliders, so for the object with the rigidbody and the isTrigger (they both were on the same object for me), I applied a simple box collider. For a RTS game I think it will be OK (for both objects).

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Destroying object when player walks over it 1 Answer

Ignore collision at high velocity. 1 Answer

Trouble with Physics.IgnoreCollision 0 Answers

Unwanted jittery behavior 2 Answers

Colliders in a wall jut out 0 Answers


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