Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by chrismferguson · Jan 12, 2017 at 10:41 PM · weaponc# tutorial

Rocket Launcher Fires Explosion, But Not Rocket

I'm following a tutorial that might be outdated at Noobtuts, so I'm not sure if the mistake is on my end or the site's, but here's my code for the rocket launcher. I get an explosion but no rocket comes out. I disabled collision on the weapon so the missile isn't exploding upon contact with the gun but maybe it still is? I don't know.

Any help would be greatly appreciated! I tried banging my head against the problem but I can't seem to solve it on my own.

The code for the rocket.

 public class Rocket : MonoBehaviour
 {
     // The fly speed (used by the weapon later)
     public float speed = 2000.0f;
 
     // explosion prefab (particles)
     public GameObject explosionPrefab;
 
     // find out when it hit something
     void OnCollisionEnter(Collision c)
     {
         // show an explosion
         // - transform.position because it should be
         //   where the rocket is
         // - Quaternion.identity because it should
         //   have the default rotation
         Instantiate(explosionPrefab,
                     transform.position,
                     Quaternion.identity);
 
         // destroy the rocket
         // note:
         //  Destroy(this) would just destroy the rocket
         //                script attached to it
         //  Destroy(gameObject) destroys the whole thing
         Destroy(gameObject);
     }
 }

The code for the weapon

 public class Shoot : MonoBehaviour {
     // Rocket Prefab
     public GameObject rocketPrefab;
     
 
     // Update is called once per frame
     void Update()
     {
         // Left mouse clicked?
         if (Input.GetMouseButton(0))
         {
             // spawn rocket
             // - Instantiate means 'throw the prefab into the game world'
             // - (GameObject) cast is required because unity is stupid
             // - transform.position because we want to instantiate it exactly
             //   where the weapon is
             // - transform.parent.rotation because the rocket should face the
             //   same direction that the player faces (which is the weapon's
             //   parent. 
             //   we can't just use the weapon's rotation because the weapon is
             //   always rotated like 45° which would make the bullet fly really
             //   weird
             GameObject g = (GameObject)Instantiate(rocketPrefab,
                                                    transform.position,
                                                    transform.parent.rotation);
 
             // make the rocket fly forward by simply calling the rigidbody's
             // AddForce method
             // (requires the rocket to have a rigidbody attached to it)
             float force = g.GetComponent<Rocket>().speed;
             g.GetComponent<Rigidbody>().AddForce(g.transform.forward * force);
         }
     }
 
 }

alt text

rocket.png (37.9 kB)
Comment
Add comment · Show 2
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 KoenigX3 · Jan 14, 2017 at 09:37 AM 1
Share

I disabled collision on the weapon so the missile isn't exploding upon contact with the gun but maybe it still is?

You can check this by simply adding a Debug.Log() function to the OnCollision call. Debug.Log(c.gameObject.name); (Be sure to write this at the beginning of the function before destroying the object.) If it is still colliding, then you have two options: try to disable collision between them using another method, or instantiate the rocket with an offset, so it will not spawn in the weapon. GameObject g = (GameObject)Instantiate(rocketPrefab, transform.position + transform.forward * z, transform.parent.rotation); Replace the 'z' with a number, and increase it until you get the right offset. If it is not spawning in front of the weapon, you should try to use transform.right, transform.up, or the negatives of these vectors.
avatar image chrismferguson KoenigX3 · Jan 15, 2017 at 06:36 AM 0
Share

I tried all of that and it still didn't work. This is probably too high level for me atm, but thank you for taking the opportunity to answer me. I truly appreciate it, thanks!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by KarenCrawford · Jan 15, 2017 at 02:06 PM

I would do the following, and I am not trying to insult you.

  1. Make sure you are assigning the rocket in the inspector for the second script and not accidentally assigning the the explosion to both scripts ( I have done sillier things)

  2. Comment out the Destroy function just to see what happens. I believe your problem could be with the location of where you are instantiated the item at. What I do is put an empty gameobject at the front of the gun, then get the vector 3 of that object and assign that to the newly instantiated item. My guess is the transform.position part of your script is what is causing the issue.

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

Answer by chrismferguson · Jan 14, 2017 at 07:40 AM

Hello, anyone?

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

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

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

How can you do borderlands style random weapons and stat generation? 2 Answers

Landmine is spawning with wrong rotation 0 Answers

Having a few script errors that I ran into. 1 Answer

char speed need help 0 Answers

software module that takes care of managing experience points and level. 0 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