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
3
Question by khh · Feb 21, 2014 at 09:01 PM · collisionrigidbodycapsulecollider

OnCollisionEnter not entered on obvious collision

Hei there

I'm quite new to Unity, and I'm having a problem. I'm spawning an empty casing prefab as part of the fireing of a gun, imparting it with some velocity and letting it fly. I then want to detect when it hits the ground and play a sound and then remove it. But the collision even never fires.

The casing has a capsule collider and a non-kinematic rigidbody on it, and I've double checked that the ground has a (non-trigger) mesh collider on it. The rigidbody starts out being a trigger, but is changed to not be a trigger soon after it's spawned in. When I play the game I can see the casing being spawned and hitting the floor (where it bounces around a bit), but OnCollisionEnter never get's called. Below is the script I've attached to the casing prefab.

 using UnityEngine;
 using System.Collections;
 
 public class CasingScript : MonoBehaviour {
 
     public float timeImmunity;
     public float timeMax;
     float timeCreation;
     bool collided = false;
 
     void Awake() {
         timeCreation = Time.time;
     }
 
     void FixedUpdate() {
         if ((Time.time - timeCreation) > timeImmunity) {
             collider.isTrigger = false;
         }
 
         if (collided && !audio.isPlaying) {
             Debug.Log("Casing destoryed");
             Destroy(gameObject);
         }
 
         if ((Time.time - timeCreation) > timeMax) {
             Debug.Log("Casing timed out");
             Destroy(gameObject);
         }
     }
 
     void OnCollisionEnter(Collision other) {
         if ((Time.time - timeCreation) < timeImmunity) {
             Debug.Log("Casing collided; immune");
             return;
         }
 
         if (!collided) {
             collided = true;
             audio.Play();
             Debug.Log("Casing collided");
         }
     }
     void OnTriggerEnter(Collider other) {
         Debug.Log ("Triggered against " + other.gameObject.name);
     }
 }
 
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 rednax20 · Feb 21, 2014 at 09:14 PM 0
Share

hmm, to much code for me to stomach right now, but whenever you want to do collisions, my rule is make sure you have colliders and rigidbodies on both objects, collision can be unpredictable otherwise. well predictable, but tricky. just test it real quick with a rigidbody on the ground with the gravity component unchecked.

avatar image khh · Feb 21, 2014 at 09:26 PM 0
Share

I did check that. It still didn't detect the collision, but it did cause the ground to move undesirably.

avatar image Zaphyk · Feb 21, 2014 at 09:52 PM 0
Share

Did you Checked the collider.isTrigger is set to false at runtime? Because maybe the condition is not true and the isTriggered option keeps being false.

$$anonymous$$aybe add a Debug line to test if condition is being done

avatar image khh · Feb 21, 2014 at 11:24 PM 0
Share

I added a OnTriggerEnter with a debug statement, it's not being called either.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by khh · Feb 22, 2014 at 12:33 AM

Seems to be a bug with the capsule collider. I changed the collider to a boxcollider without changing anything else, and then it suddenly started working as expected.

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
avatar image
0

Answer by Maximillion · Feb 21, 2014 at 10:06 PM

Have you directly added in a Debug for the OnCollisionEnter?

 void OnCollisionEnter(Collision other) {
     if ((Time.time - timeCreation) < timeImmunity) {
         return;
     }
  
     collided = true;
     audio.Play();
     Debug.Log("Bullet Hit");
 }

If that is shown, there is a possibility that the " && !audio.isPlaying" is causing the issue. But let me know whether you have and give it a go if not.

Comment
Add comment · Show 1 · 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 khh · Feb 21, 2014 at 11:33 PM 0
Share

I tested that, and updated the code. OnCollisionEnter isn't being called at all, even though I can see the object hitting the ground.

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

21 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

Related Questions

Collision between RigidBody and Capsule Collider not producing desired deflection 1 Answer

OnCollisionEnter and rigidbody problem 1 Answer

Character falls through floor any suggestions? 1 Answer

GameObject sometimes collides, but sometimes goes through each other. 0 Answers

Why my rigidbody object doesn't go straight? 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