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);
}
}
}
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.I disabled collision on the weapon so the missile isn't exploding upon contact with the gun but maybe it still is?
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!
Answer by KarenCrawford · Jan 15, 2017 at 02:06 PM
I would do the following, and I am not trying to insult you.
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)
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.