- Home /
Firing a rocket - flight problem
Hey, trying to instiate a rocket, but at the moment, it's just spawning hundreds and hundreds of rockets without me even pressing the mouse, it nearly crashes unity if I don't stop playmode.
using UnityEngine;
using System.Collections;
public class RocketLauncher : MonoBehaviour {
public GameObject thingToSpawn;
void Update () {
if(Input.GetKeyDown(KeyCode.Space));
{
Instantiate(thingToSpawn, transform.position, transform.rotation);
}
}
}
The problem with the flight mode is that I've got a small script setup, it banks left and right with ease along the Z axis, and it rotates on the X forwards and backwards, but if I rotate it along the Z 90degreas, it won't rotate on the X at all.
(Using relative torque.)
Is this script attached to the rocket prefab? If so, maybe the newly created rocket "sees" space pressed and create a new one, which will create another clone etc. The flight problem may be caused by the famous gimbal lock: when rotating 90 degrees around z, x aligns with y, and rotation around x becomes impossible (it depends on how you're doing the rotation).
Answer by Bicko · Jan 20, 2012 at 03:32 PM
You've got a semicolon at the end of your if statement, so Unity thinks the statement has ended and then Update() sees this lovely little Instantiate command to run every frame. Simple problems are the hardest to spot!
Answer by Ranger-Ori · May 07, 2012 at 08:39 AM
When you script with GetKeyDown, it means that as long as you press the key, it keeps instantiating. You have to put an Yield function in order to "pause" the spawning of your rockets. It also seems that you should apply forces to the rocket in order to make it fly.
Your answer
Follow this Question
Related Questions
Create plane from 2 Vectors of a symmetry line segment 1 Answer
Rotation troubles 2 Answers
Shoot an object and have it move based on rotation 1 Answer
Instantiate not working 2 Answers