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 AntoK · Apr 05, 2014 at 08:08 PM · c#collisionscollisiondetectioncollision-error

OnCollisionEnter not working for bullet

HI all,

I have created an object that fires some bullets from a tutorial but I can figure out why the OnCollisionEnter event isn't firing when the bullet object hits a wall, I just want to destroy the bullet when it hits the wall. This is the bullet class that I have applied to an empty object which has a prefab linked to it. public class Bullet : MonoBehaviour {

         public float moveSpeed = 40f;
         public Rigidbody projectile;    
         public Rigidbody clone;
 
         // Use this for initialization
         void Start () 
         {
 
         }
 
         // Update is called once per frame
         void Update () 
         {
             if (Input.GetKey (KeyCode.Space))
                 Shoot ();
         }
         
         void Shoot()
         {
             clone = (Rigidbody)Instantiate (projectile, transform.position, transform.rotation);
             clone.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
             clone.detectCollisions = true;
             clone.velocity = transform.TransformDirection(new Vector3(0,0,moveSpeed));
             Destroy (clone.gameObject,3);
 
         }
 
         void OnCollisionEnter(Collision collision) 
         {
             Destroy (clone.gameObject);            
         }

The bullets shoot ok and stop when they hit the wall but even when debugging the OnCollisionEnter method isn't called.

Any help would be great. Thanks, Anto

Comment
Add comment · Show 3
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 0V3RR1D3 · Apr 06, 2014 at 02:14 AM -1
Share

Have you set the collision box to "Is trigger"?

avatar image getyour411 · Apr 06, 2014 at 04:11 AM 0
Share

If your debug shows OnCollision is not called then perhaps your Wall does not meet the requirements for a collision?

avatar image AntoK · Apr 06, 2014 at 08:49 AM 0
Share

The wall has the BoxCollider on it by default and all of the bullets do actually stop when they hit the wall but for whatever reason the method isn't called. I was thinking it is because the OnCollisionEnter is a member method of Bullet but I'm not actually creating an instance of the Bullet class, I'm creating a variable and assigning a prefab to it. I don't fully understand how Unity handles the scripts being applied to an object so maybe I'm not thinking about it in the right way.

2 Replies

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

Answer by Philtermango · Apr 06, 2014 at 09:13 AM

The OnCollisionEnter will only be called on scripts that are attached to the wall or bullet. The script you posted is not attached to either so OnCollisionEnter is not called. That Bullet script just instantiates the bullet and sends it on its way. Add OnCollisionEnter to a script attached to the bullet prefab or wall and it should work.

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 AntoK · Apr 06, 2014 at 12:25 PM 0
Share

That was it alright, noob mistake, thanks!

avatar image
0

Answer by QuantumMacgyver · Apr 06, 2014 at 04:39 AM

If the bullets are moving fast enough, they can sometimes pass entirely though the wall in between physics updates. (Slow them waay down and see if the problem's still there. If it is, ignore the rest of this post.)

Assuming that fixed it, all you need to do is have your bullets raycast ahead of themselves and run your destroy script when they either hit the target or would hit it by the next frame.

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 AntoK · Apr 06, 2014 at 08:43 AM 0
Share

Thanks for the reply Quantum$$anonymous$$acgyver, but the bullets are already moving very slowly, every bullet hits the wall and stops correctly but the OnCollisionEnter method above isn't called when the bullet hits the wall. I'll look into the raycasting for the bullets, was just trying to keep it simple for a intro to Unity.

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

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

Collider colliding with itself 1 Answer

How to make a certain function if an object collides with another object? Been searching it up for 2 months. 1 Answer

How to check if a collision impact is perpendicular with the velocity of the collider? 0 Answers

Zero collision responsiveness - C# 1 Answer

Make collision work when animation is playing? c# 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