- Home /
Space Craft controller.
I have the following code for my space craft to allow it to fly in space but right now when I press the key to make it move it continues to move in that direction even after you relase the key. I want to change this code to make it so that once the button is released it causes the ship to stop moving in that direcion to give more of a exact contolle.
var Speed = 1000;
var TorqueSpeed = 20.0;
function Update () {
if(Input.GetKeyDown(KeyCode.W)){constantForce.relativeForce.z = Speed * Time.deltaTime;else{
if(Input.GetKey(KeyCode.S)){constantForce.relativeForce.z = -Speed * Time.deltaTime;}else{
constantForce.relativeForce.z = 0;}}
//Turn right and left
if(Input.GetKey(KeyCode.D)){constantForce.relativeTorque.y = TorqueSpeed * Time.deltaTime;}else{
if(Input.GetKey(KeyCode.A)){constantForce.relativeTorque.y = -TorqueSpeed * Time.deltaTime;}else{
constantForce.relativeTorque.y = 0;}}
//Rotate Up and Down
if(Input.GetKey(KeyCode.E)){constantForce.relativeTorque.x = TorqueSpeed * Time.deltaTime;}else{
if(Input.GetKey(KeyCode.Q)){constantForce.relativeTorque.x = -TorqueSpeed * Time.deltaTime;}else{
constantForce.relativeTorque.x = 0;}}
//Rotate right and left
if(Input.GetKey(KeyCode.Z)){constantForce.relativeTorque.z = TorqueSpeed * Time.deltaTime;}else{
if(Input.GetKey(KeyCode.X)){constantForce.relativeTorque.z = -TorqueSpeed * Time.deltaTime;}else{
constantForce.relativeTorque.z = 0;}}
}
A real spacecraft will have momentum, right? $$anonymous$$eaning that even if you turn of your thrusters, you will continue moving at the same speed. Are you trying to make your spacecraft stop when you release the keys?
yes I would like to make it so that it has a break a little bit so that you dont have to fight quite so hard to get controlle back of it.
$$anonymous$$aybe what you want is drag. Have you tried adding more drag (and angular drag) to your rigidbody?
I think this should be done in FixedUpdate.
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.FixedUpdate.html
I gave every one here credit because I ended up using a little of what all of you said to make it work.
Answer by Griffo · Oct 09, 2012 at 03:52 PM
I think you need
Input.GetKeyDown
I tried that on the top one with the w and it dont seem to make a difference.
I gave every one here credit because I ended up using a little of what all of you said to make it work.
Your answer
Follow this Question
Related Questions
Deactivate Controlle of one object and Contoll another 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
PinBall Flippers 1 Answer
Enemy AI script causing Unity to crash (Javascript to C#) 1 Answer