x


How could I use the mouse as input for a flight control system (and space bar for thrust!)

How would i go about making a plane-like thing, where the mouse pointer controls direction, and space bar controls thrust?? If anyone can help with a script i could expand on that'd be great. I have tried this now:

var x = Input.GetAxis ("Mouse X");
var y = Input.GetAxis ("Mouse Y");

function FixedUpdate () {
rigidbody.AddRelativeTorque(x,0,0);
rigidbody.AddRelativeTorque(0,0,y);
}

But It didn't work. Any Ideas?

more ▼

asked Apr 16 '10 at 03:05 PM

Fishman92 gravatar image

Fishman92
2.4k 101 113 128

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Here:

fuction Start() { 
    Screen.lockCursor = true;
}    

function FixedUpdate() {
   // Turn
   var h = Input.GetAxis("Mouse X");
   var v = Input.GetAxis("Mouse Y");

   rigidbody.AddRelativeTorque(0, h * 20, v * 20);
   // Other way you may like
   //transform.Rotate(0, h * 20, v * 20);

   // Thrust only if Space is held down
   if (Input.GetKey(KeyCode.Space)) rigidbody.AddForce(transform.forward);
}

Also if transform.forward doesn't work try transform.right

Oh and if you want to be able to rotate:

    var rotateAmount = 5.0;
    function Update() {
       if (Input.GetAxis("Horizontal") < 0) { transform.Rotate(rotateAmount, 0, 0); }
       else if (Input.GetAxis("Horizontal") > 0) transform.Rotate(-rotateAmount, 0, 0); }
    }

To rotate use left/right arrow keys or A/D

more ▼

answered Jul 13 '10 at 07:15 PM

fireDude67 gravatar image

fireDude67
945 9 15 30

what language is the above code written in? I get compiler errors for this script.

Error 1: A namespace can only contain types or namespace declarations. Error 2: Parsing Error

They both reference your first line of code:

fuction Start() {

Any help is greatly appreciated. Thanks.

Feb 12 '12 at 11:09 AM shawnkilian

That's in JavaScript. The error is because "function" is misspelled.

Apr 29 '12 at 04:06 PM nixtwiz
(comments are locked)
10|3000 characters needed characters left

This is pretty much a duplicate question (except for the mention of mouse/space bar for the controls), so see this answer for more detail about how to implement flight physics in general.

To read the Mouse movement, you'd want to lock the cursor, and use Input.GetAxis to read the horizontal and vertical movements of the mouse, like this:

var x = Input.GetAxis ("Mouse X");
var y = Input.GetAxis ("Mouse Y");

You'd then want to use the "x" variable as the basis for the amount of torque you apply to control banking (as mentioned in the answer linked above), and the "y" value to control the amount of torque applied for *pitch control.

To read whether the space bar is currently pressed, use:

if (Input.GetKey(KeyCode.Space)) {
    // space is pressed - apply force here
}
more ▼

answered Apr 16 '10 at 03:21 PM

duck gravatar image

duck ♦♦
41k 92 148 415

Um, how would i link up the mouse part to actually controll the banking? Any example?

Apr 16 '10 at 10:23 PM Fishman92

Check my answer!

Jul 13 '10 at 07:16 PM fireDude67
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x983
x953
x224
x58

asked: Apr 16 '10 at 03:05 PM

Seen: 3446 times

Last Updated: Apr 29 '12 at 04:07 PM