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 vscioli · Jan 18, 2014 at 12:57 AM · destroyprefabsprojectiletarget

Trying to destroy a projectile and "teleport" to another area of the screen when it hits a target.

Hey again guys! I've been pretty active here and I am learning alot by asking you guys for help! I'm still working on another problem with @Quillict, but that's another story. Anyway, my question is this:How do I go about destroying a projectile after it hits a target? This is the set up, so you can understand my code better. I am shooting a cannonball out of a cannon automatically as the scene loads. I got that part of the script right. Now, on the right of the screen I have a Target/bullseye, tagged "Target" that moves up and down at a fixed speed. What is supposed to happen is that once the ball hits the target, it gets destroyed (I'm not going to use preset animation) and gets teleported to the upper right of the screen and falls down, like if the are being collected. Here is my code thus far. Mind you that the fire function was the original code with InvokeRepeating on the outside (moved it inside). Everything else, Update() and OnEnterCollision (), are new.

 #pragma strict
 
 //Attach to empty object, in front of cannon barrel  
 var projectile : Rigidbody; //The cannonball
  
 var fireRate = 2;//Time Delay Between cannonballs
  
  
 function Update()
 
 {
     fire();
     OnCollisionEnter();
 
 }
 
 //dictates the sound effect,and spawns the cannonball at a random velocity
 function fire()
  
 {
  
     audio.Play();
     
     var gunShot : AudioClip;
     
     var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation );
      
          instantiatedProjectile.velocity = transform.TransformDirection(Vector3(0,0,Random.Range(1,5)));
      
     audio.PlayOneShot(gunShot);
     
     
 InvokeRepeating("fire",2,fireRate); //instructions to repeat cannonballs firing
 
 }    
 @script RequireComponent(AudioSource)
 
 //this checks if the object you hit is the object tagged "Target"
 function OnCollisionEnter(collision : Collision)
 
 {
     if(collision.gameObject.tag == "Target")
     {
         //destroys cannonball
         Destroy(collision.rigidbody);
     }
 }

Thoughts? Thanks in advance for the help!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by komodor · Jan 18, 2014 at 01:47 AM

not sure if you are looking for this, but shoudln't you call Destroy(collision.gameObject)?

Comment
Add comment · Show 1 · 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 vscioli · Jan 18, 2014 at 04:18 AM 0
Share

hmmmm. I have the projectile set as a ridgidbody though? Does that matter?

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

18 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 avatar image avatar image avatar image avatar image

Related Questions

Collision between 2 prefabs. 0 Answers

The AI-Players do not see new install (Instantiate) object (the ball) after they set the first goal in gates and old object (the ball) was destroyed. 0 Answers

Destroy all Objects of a Type in Game 1 Answer

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

c# destroy Gameobject on 0 hp 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