- Home /
Why won't my bullets detect a collision and be destroyed?...
Hello everyone, I am very new to these forums but have been messing with Unity for a while now. I am trying to make a simple game just playing around (basic primitives, etc.). However I have already hit a wall.... In the game I'm making a blue cube shoots a little blue ball at a red cube. I would like the little blue ball to disappear when it collides with anything; the red cube, something else, myself, etc. This little blue bullet shoots slowly so I don't want to use RayCasts because I want to see the bullet at all times. I have it now as a rigidbody, unaffected by gravity so that it doesn't drop as it flies through the air. No matter what I do and no matter how hard I search I cannot find a way to make it detect the collision and destroy itself! All of the collision scripts add in explosion prefabs and sounds and things, but all I want is for the cube to simply vanish when it hits another object. Be it the character with a charactercontroller or a rigidbody. I will now insert two of the scripts I am using for the bullet prefab, if someone can alert me to a means of posting the whole scene on the internet or on this site for better clarity I would gladly post it.
This first code is to make the bullets disappear if they don't hit something
var lifespan= 5;
function Update () {
Destroy(gameObject,lifespan);
}
The next code is what should destroy the bullet(clone) object once it hits something.
function OnCollisionEnter(collision : Collision){
Destroy(gameObject);
}
Sure is funny how things can go wrong with such simple things! Thank you in advance for the help!
Answer by JonManatee · Jun 23, 2010 at 02:53 AM
I don't see any problems with your code, other than that you may not want to have Destroy(gameObject, lifespan) in Start instead of Update
Some simple troubleshooting to help you along:
- If there are compile errors in any of your scripts, Unity may revert to an older version until the bugs are fixed (the version where they don't disappear).
- Make sure your scripts are attached to a parent of the renderer
- Make sure that you have a collider attached to the GameObject with the scripts on it.
- Make sure the objects you want it to collide with have colliders
Answer by HardStyle · Apr 04, 2012 at 05:54 PM
another problem may be if your bullet is traveling 2 fast, if for 1 update the speed of bullet is bigger then a wall it will pass ....you need to use raycasts to fix this, not colisions
Answer by BoredKoi · Jun 23, 2010 at 12:02 PM
I cannot find a way to make it detect the collision and destroy itself!
I recently helped another person on the forums with similar questions in understanding the usage between Collisions and Triggers, perhaps that will help (about 10 posts down on the first page of the thread): Unity Scripting Forum post
The matrix at the bottom of the collider pages is what you need to review; if you don't understand how Static and isTrigger (Colliders) and isKinematic (rigidbodies) change those behaviors then you'll spend a lot of time with frustrating trial and error.
Hope you find this useful!
Answer by Panamamigo · Jun 24, 2010 at 08:03 PM
I'm not exactly sure what I was doing wrong either, but I just started a completely new project and re-attempted everything and it works perfect!
Thank you all for your responses they have been very helpful. :)