- Home /
Bullets wont collide and destroy object
I am having a problem with my code for some reason. When I shoot my bullets, the object I am shooting at will collide but will not destroy the object or cause an explosion. Can someone please help me with the code?
var speed : float = 30; var explosion : GameObject;
function Start () {
Invoke("Destroy_Bullet", 1.0);
}
function Update () {
transform.Translate (Vector3.right * speed * Time.deltaTime);
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "shooter") {
Instantiate (explosion, transform.position, transform.rotation);
Destroy(gameObject);
print("hit");
}
}
function Destroy_Bullet(){
Destroy(gameObject);
}
There are 2 functions for you can use for collisions OnTriggerEnter and OnCollisionEnter. You should look each up and read the differences. I believe you want to use OnCollisionEnter. Or perhaps your bullet isnt not reaching the "shooter" before you destroy it. I believe calling Invoke("Destroy_Bullet", 1.0); will call the Destroy_Bullet function after 1 second.
Answer by Turbine · Apr 18, 2013 at 08:05 PM
A couple things to check
Check to make sure your bullet's collider is set to trigger mode.
Make sure one of the two objects has a rigidbody or however it is the doc's suggest
Bullets tend to move quickly, it may be passing through the game object. Either make the bullet collider bigger, slow down the bullet, or turn on interpolate. Interpolation is probably not a good idea seeing as you will probably have a lot of bullets.
If you use triggers, you are going to have to manually find the real impact point to create the explosion from
Make sure the game object you are shooting and the bullets are in physics layers that can hit each other.
Make sure your tag's make sense. You are checking if the tag is equal to shooter, I have a hunch it should be 'not equal to' instead
-(Edit)You are not destroying the explosion through this code. Unless you have code elsewhere that destroys the explosion, you will have to edit the line that spawns the explosion to be as follows:
Destroy(Instantiate(myExplosion,hitPosition,hitRotation),1);
Thanks. But I have a little problem. I have gotten the object to disappear when it collides with the bullet but the explosion does not work. When it collides, the explosions stays in it position but it does not explode.
Here is the code:
function start(){
Invoke("Destroy_Explosion", 3);
}
function Destroy_Explosion(){
Destroy(gameObject);
}
function Start() : note the capitol S
You can also use a delay with the Destroy command :
// $$anonymous$$ills the game object in 5 seconds after loading the object
Destroy (gameObject, 5);
http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
function Start()
{
Destroy( gameObject, 3 );
}
Please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Your answer
Follow this Question
Related Questions
Tank Missle Colide then explode and destroy 1 Answer
Objects do not destroy on Collision 1 Answer
How to destroy objects only when they collide with other objects? 1 Answer
Makeing something Blow-up 3 Answers
Scoring (i'm stuck) 3 Answers