- Home /
Missile launcher is malfunctioning
Hello,
I am working through the old FPS Tutorial, and everything has been going smoothly up until this point. I have managed to create a launcher that launches the "Missile" GameObject, which is a grey sphere. My problem arose when the time came to assign a GameObject to the "Explosion" variable in the "Projectile" script (Projectile.js was a component of Missile.prefab).
Basically, this is what happens: I fire with Left-click (Fire1) and it simply spawns an explosion at my character (the capsule). It SHOULD be launching the sphere and then exploding upon collision with other objects.
I can provide code if it helps, but I am stuck at this point and do not know what to do.
Thank you!
In general, yes, post code when asking code questions.
Is it possible that your missile is colliding with the player or the launcher? Or that your explosion effect is active already and thus explodes as soon as it is spawned? $$anonymous$$ore information will definitely help.
Okay, here is the code.
Projectile.js
var explosion : GameObject;
function OnCollisionEnter( collision : Collision )
{
var contact : ContactPoint = collision.contacts[0];
var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal );
var instantiatedExplosion : GameObject = Instantiate(
explosion, contact.point, rotation );
Destroy( gameObject );
}
Code for Explosion.js
var explosionTime = 1.0;
function Start()
{
Destroy( gameObject, explosionTime );
}
Code for $$anonymous$$issileLauncher.js
var projectile : Rigidbody;
var speed = 20;
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity =
transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider );
}
}
The commenting section messed up my layout. Anyways, do you know how I can resolve this issue? You mentioned something about it beco$$anonymous$$g active and exploding immediately, so is there a way for it to become active upon collision?
If you look at the code, when OnCollisionEnter is called on the Projectile the explosion gets instantiated. As I suspected, your missile is likely colliding with something as you're firing it.
Answer by Soozle · Nov 25, 2013 at 09:44 PM
Ah, I fixed it! All I had to do was move the Launcher object further away from the Capsule so it didn't collide. Thanks for the assistance! Please close.
Answer by Commander Quackers · Nov 26, 2013 at 03:21 AM
I suspect that your missile is colliding with the launcher or the player. In order to make this work, you should make it so that the projectile is not colliding or on a path of collision with the launcher/player. For instance, if the projectile is colliding with the launcher or player as it spawns, it will make it explode near you. If it wasn't colliding, it would wait for a collision (wherever it's being fired to) to explode.
Also are you getting any errors by any chance?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Particles are off when I shoot 1 Answer
Enemy Collision Wont Work! PLEASE HELP 0 Answers
how to morph character on collision enter with gameobject 1 Answer
Enemy deals damage to player on contact 2 Answers