• 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 slidecoast · Apr 18, 2013 at 06:12 PM · collisiondestroyexplosionbullets

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);

}

Comment
Add comment · Show 1
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 PAEvenson · Apr 18, 2013 at 07:11 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
Comment
Add comment · Show 2 · 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 slidecoast · Apr 19, 2013 at 07:14 PM 0
Share

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);

}

avatar image AlucardJay · Apr 19, 2013 at 07:41 PM 0
Share

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.

  • http://answers.unity3d.com/page/newuser.html

  • http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

14 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

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

Collision Detection Issue with SetActive(false) not working 2 Answers

"Destroy" bullet without turning off trail renderer 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges