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 Henri 1 · Dec 13, 2011 at 09:14 PM · collisionphysicscollidercollision detection

Best collision detection method?

Good day everybody!

Hope all is well :)

I'm having some problems with my simple collision script, and was wondering if I'm doing it the right way.


Heres the scenario:

  • Have a "spell" which my player casts, it instantiates a gameObject with a particle system, light, collision script, rigidbody, sphere collider and detonator attached to it (Sphere collider disabled on "source", and enabled once "cast")

  • When my player "casts" the spell, everythings works 100% except for the collision sometimes.

  • My rigidbody's collision detection is set to "Continues Dynamic", does not make a difference when set to "Dynamic" or "Discreet"

  • I require the rigidbody in order to add force for the spell to move in the direction shot


Heres what happens:

  • The instantiated object does bounce off my world (meaning the collision is working)

  • Sometimes it does print "Hit" to the console, but does not explode

  • Sometimes it prints "Hit" and explodes, but does not detroy the object

  • Sometimes it does not print or explode, but destroys itself

  • Sometimes it prints, explodes and destroys.

  • Angle/direction the spell is cast in does not effect the result.


Herewith my script attached to the "source":

 function OnCollisionStay(collision : Collision) {
     print ("Hit!");
     SendMessage("doExplode", SendMessageOptions.RequireReceiver);
     Destroy(gameObject);
     }



I also tried OnCollisionEnter but does not detect anything.

Quite possibly, I'm missing something, and hope that someone more experienced will be able to give me an opinion/solution.

All the best!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by gregzo · Dec 13, 2011 at 09:28 PM

What do you want the object to do? If you want it to explode at first collision, use OnCollisionEnter. What is it colliding with? Very strange if OnCollisionEnter doesn't detect anything. Especialy since you say your spell bounces: each bounce should be detected. Try print(collision.gameObject.name) and if it doesn't give you the name of the object/surface your spell bounces off, your problem probably lies elsewhere. Try debugging with OnCollisionEnter(collision:Collider) as well, to narrow it down. Ah, one more thing: you say your script is attached to the "source", I hope that doesn't mean it just stays with the caster and hence doesn't travel with the spell object!

Comment
Add comment · Show 4 · 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 Henri 1 · Dec 13, 2011 at 09:38 PM 0
Share

Hi @gregzo,

Thanks for the reply. With "print(collision.gameObject.name)", it does print the name of the object its colliding with, but again only sometimes, and again does not send the message or destroys itself. When using "OnCollisionEnter(collision : Collider)" i get an error saying "Script error: OnCollisionEnter - This message parameter has to be of type: Collision - The message will be ignored."

The "source" object gets instantiated, basically duplicated with all the attached scripts and components, did check and script is there

avatar image gregzo · Dec 13, 2011 at 09:43 PM 0
Share

Sorry for the typo, i meant OnTriggerEnter(coll: Collider) to check of you had some colliders unknowingly set to isTrigger.

avatar image gregzo · Dec 13, 2011 at 09:46 PM 0
Share

What do you mean by source? A parent object? In what object is the OnCollisionEnter function? Just making you check the obvious, sorry, but that's often where mistakes hide...

avatar image Henri 1 · Dec 13, 2011 at 09:59 PM 0
Share

No prob. Only have 2 colliders thus far. The world (mesh collider) and the instantiated object, none set to isTrigger. By source I mean the object that gets instantiated, as that object is not visible (renderer and emitter turned off), only the instantiated object (renderer turned on and emitter on upon instantiation).

The "collision" script file is attached to my source object, and is included in the cloned object.

Just created a plane with a box and relevant colliders, getting same problems, so my current world and its collider is not problematic

avatar image
1

Answer by ChefZweegie · Dec 13, 2011 at 09:52 PM

Just ran into the same problem and came up with a solution that works in my case. My collision was doing the same thing where it would work sometimes and other times it would not. In order to fix this, I had to make sure that both the collider and the object it was colliding with have a rigidbody in it. The problem came up more when one of the objects was already involved in another collision. Try that and let me know.

Comment
Add comment · Show 3 · 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 Henri 1 · Dec 14, 2011 at 03:26 PM 0
Share

Thanks man... it worked... never thought of that :)

avatar image Henri 1 · Dec 14, 2011 at 03:44 PM 0
Share

But again some collisions are not detected (about 1 in every 20) :(

avatar image ChefZweegie · Dec 14, 2011 at 10:29 PM 0
Share

Try to change the collision method. If that does not work, post up some code. $$anonymous$$aybe the object is skipping over the collision every so often. I'd slow the object down to see if this is the problem. If you need the object to travel fast, look into a ray cast to help deter$$anonymous$$e to collision.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

OnCollisionEnter isnt called when player lands on object 1 Answer

No Collision 3 Answers

Detect collider collision ,identify and access colliding colliders 0 Answers

Physic based golf game - ball bouncing off the connection of colliders on flat surface 2 Answers

Avoid Player bouncing when colliding with objects 3D 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