- Home /
Missile Firing Script Help
I have the following missile script. It should shoot a missile, but whenever I press the defined key, nothing happens. Help?
public var projectile : Rigidbody;
function Update () {
if (Input.GetKeyDown("enter")) {
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection (Vector3.forward * 10);
}
}
Answer by clunk47 · Aug 07, 2013 at 06:08 PM
"enter" is for The enter button on the keypad. Are you hitting that, or are you pressing the enter key next to the " key? If you want to use that button, you need to use "return". If you want to use either or,
if(Input.GetKeyDown("enter") || Input.GetKeyDown("return"))
{
//Code here
}
Ahh... I see. That's a bit weird, but it works. Would it be possible to destroy it upon colliding with an object?
Of course. Just have a look at OnCollisionEnter and Destroy.
I would just attach a script w/ OnCollisionEnter and Destroy on the missile prefab itself, something like:
function OnCollisionEnter(collision : Collision)
{
Destroy(gameObject);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613111723im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GuiTexture Width Change 1 Answer
Space Invaders movement? 1 Answer
Script to disable MouseLook? 2 Answers
Help needed with a pickup script 2 Answers