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

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

Makeing something Blow-up 3 Answers

Scoring (i'm stuck) 3 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