Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by HolBol · Apr 16, 2010 at 03:05 PM · inputmousecontrolflight

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by fireDude67 · Jul 13, 2010 at 07:15 PM

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Velketor · Feb 12, 2012 at 11:09 AM 0
Share

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.

avatar image nixtwiz · Apr 29, 2012 at 04:06 PM 0
Share

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

avatar image meekinscot · Jan 05, 2016 at 09:39 PM 0
Share

Sorry to ask on such an old thread but this is exactly what I need. I get a problem with the line:

rigidbody.AddRelativeTorque(0, h 20, v 20);

The spaces between h 20 and v 20 give me errors but I'm not sure what should be there ins$$anonymous$$d

avatar image
3

Answer by duck · Apr 16, 2010 at 03:21 PM

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
}
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HolBol · Apr 16, 2010 at 10:23 PM 0
Share

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

avatar image fireDude67 · Jul 13, 2010 at 07:16 PM 0
Share

Check my answer!

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

Implementing a virtual joystick with mouse 0 Answers

How do I use the mouse to control a spacecraft like in this game? 1 Answer

How can I treat mouse movement as joystick input? 1 Answer

Character Zoom in and out 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges