x


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!

more ▼

asked Jun 23, 2010 at 02:43 AM

Panamamigo gravatar image

Panamamigo
156 33 26 36

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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:

  1. 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).
  2. Make sure your scripts are attached to a parent of the renderer
  3. Make sure that you have a collider attached to the GameObject with the scripts on it.
  4. Make sure the objects you want it to collide with have colliders
more ▼

answered Jun 23, 2010 at 02:53 AM

JonManatee gravatar image

JonManatee
266 21 15 28

(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Jun 23, 2010 at 12:02 PM

BoredKoi gravatar image

BoredKoi
451 9

(comments are locked)
10|3000 characters needed characters left

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. :)

more ▼

answered Jun 24, 2010 at 08:03 PM

Panamamigo gravatar image

Panamamigo
156 33 26 36

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Apr 04, 2012 at 05:54 PM

HardStyle gravatar image

HardStyle
0 3 2 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4164
x1216
x498
x430
x69

asked: Jun 23, 2010 at 02:43 AM

Seen: 7660 times

Last Updated: Apr 04, 2012 at 05:54 PM