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 AlexFili · Sep 11, 2012 at 01:12 AM · destroyprojectileapplezombie

Making projectiles disappear when they hit the ground

I've got this prototype game where the player throws apples at zombies. The problem is I haven't worked out a good bit of code to destroy the apples upon contact with the ground.

http://www.mediafire.com/?wgkqdvfoaqn73ew

Alternatively, it would be great if the apples could de-spawn after a period of say, 2 seconds. Would each apple needs its own time variable? Many thanks.

Thank you everyone. I managed to solve my problem!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by skalev · Sep 11, 2012 at 03:39 AM

Put a collider on the floor.

Have a script on the apple, and OnCollisionEnter() yield for how long you want to wait. Then call destroyObject.

Comment
Add comment · Show 6 · 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 AlexFili · Sep 11, 2012 at 09:25 AM 0
Share

Tried that but for some reason the apples stop spawning. $$anonymous$$aybe it's because it's firing from within an object. $$anonymous$$aybe if I position the apple away from the character as its fired.

avatar image zerox911 · Sep 11, 2012 at 09:26 AM 0
Share

if u have an instantiate code...

dont use destroy(gameObject) ins$$anonymous$$d use DESTROY(gameObject.Find("YOUR OBJECT(clone)");

if u use destroy(gameObject).. it will have errors because there is no more prefab to instantiate..

else if u use destroy(gameObject.Find("your object(clone)").. it only destroy the clone..

avatar image AlexFili · Sep 11, 2012 at 09:32 AM 0
Share

I see. Thanks

avatar image AlexFili · Sep 11, 2012 at 09:41 AM 0
Share

Doesn't seem to work. Prefab name Apple2b, code as follows:

Destroy(gameObject.Find("Apple2b(clone)"));

avatar image 3agle · Sep 12, 2012 at 08:38 AM 1
Share

Ins$$anonymous$$d of using yield and then Destroy, you can use the 2nd parameter of Destroy.

Destroy("object", 2);

Would destroy in 2s.

Show more comments
avatar image
0

Answer by MickM · Sep 11, 2012 at 04:59 AM

I did this pretty easily with a bow and arrow game; you obviously have it working however to put in in perspective (in case you do projectile stuff differently) I had a script attached to the bow/person that handles the firing (ie. instantating the prefab) and the prefab has its own script attached to it.

The prefab script is where you handle the despawn stuff - you can de-spawn your projectile by using

 Destroy (gameObject) 

in the script. If you want to de-spawn it after it hits something you put the de-spawn command inside

 function OnCollisionEnter(collision : Collision){
 
 }

so an example - you want to despawn it 5 seconds after hitting something - make a script called AppleFlight.js (for example) with the following code:

 var despawnTime = 5;
 
 function OnCollisionEnter(collision : Collision){
      yield WaitForSeconds(despawnTime);
      Destroy(gameObject);
     }

Then drag this script onto your apple prefab - should work as planned! Hope it helps!!!

Also:

For info you can use the collision for other stuff as well - the 'collision' variable is what it collided with - eg. my arrow script i wanted the arrow to stick into something if it collided with it (including moving targets) so i added the following into the OnCollisionEnter function :

 transform.parent = collision.transform; //The arrow parent is now what it collided with
 rigidbody.isKinematic = false; //Freeze position of arrow
 rigidbody.collider.enabled = false; //turn off collider
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 AlexFili · Sep 11, 2012 at 09:25 AM 0
Share

Tried that but after that amount of time all the apples disappear and spawning new apples stops working. How do I get the despawnTime to work for each individual apple, and not all of them?

avatar image MickM · Sep 11, 2012 at 10:42 PM 0
Share

Where have you attached the scripts? And what is in the projectile script?

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

11 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

Related Questions

A node in a childnode? 1 Answer

Scripts not on instantiated GameObjects? 1 Answer

Calculate Height of Trajectory : Unity3d 1 Answer

my zombie keeps going through me and walls 2 Answers

i want to destroy a projectile after it passes a limit on x axis after instantiation(2d game) 1 Answer


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