Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 PoZo · Aug 31, 2010 at 04:43 PM · mousekeyboardcontrolflyingmousemove

Space Flying Script Using Mouse

So I made this script that works quite well for flying around in space like conditions. I getting quite sick of having to use my keyboard for all the movement functions though. Is their any way I have swap out pitch and yaw for mouse controls?

Here's the script:

private var pilot : Rigidbody;

var invertPitch : boolean = true; var invertYaw : boolean = false; var sensitivity : float = 1.0;

function Start () { pilot = GameObject.FindWithTag("Player").rigidbody; thrustEmitter = GetComponentInChildren(ParticleEmitter); thrustEmitter.enabled = false; } function Update () { // Did the user press fire? if (Input.GetButton ("Fire1")) BroadcastMessage("Fire");

 if (Input.GetKeyDown("w"))
 {
     SetThrust(500);

 }

 if (Input.GetKeyUp("w"))
 {
     SetThrust(0);

 }

 if (Input.GetKeyDown("s"))
 {
     SetThrust(-300);    

 }

 if (Input.GetKeyUp("s"))
 {
     SetThrust(0);
 }

 if (Input.GetKeyDown("a"))
 {
     SetRoll(20);    

 }

 if (Input.GetKeyUp("a"))
 {
     SetRoll(0);

 }

 if (Input.GetKeyDown("d"))
 {
     SetRoll(-20);   

 }

 if (Input.GetKeyUp("d"))
 {
     SetRoll(0);

 }

     if (Input.GetKeyDown("i"))
 {
     SetPitch(15);

 }

 if (Input.GetKeyUp("i"))
 {
     SetPitch(0);

 }

 if (Input.GetKeyDown("k"))
 {
     SetPitch(-15);  

 }

 if (Input.GetKeyUp("k"))
 {
     SetPitch(0);

 }

     if (Input.GetKeyDown("j"))
 {
     SetYaw(5);  

 }

 if (Input.GetKeyUp("j"))
 {
     SetYaw(0);

 }

 if (Input.GetKeyDown("l"))
 {
     SetYaw(-5); 

 }

 if (Input.GetKeyUp("l"))
 {
     SetYaw(0);

 }

}

function SetThrust ( thrustForce : int) { pilot.constantForce.relativeForce = Vector3.forward thrustForce sensitivity; }

function SetRoll (rollForce : int) { pilot.constantForce.relativeTorque = Vector3(0,0,1) rollForce sensitivity; }

function SetPitch (pitchForce : int) { if(invertPitch) { pilot.constantForce.relativeTorque = Vector3.right pitchForce sensitivity; } else { pilot.constantForce.relativeTorque = Vector3.left pitchForce sensitivity; } }

function SetYaw (yawForce : int)
{ if(invertYaw) { pilot.constantForce.relativeTorque = Vector3(0,1,0) yawForce sensitivity; } else { pilot.constantForce.relativeTorque = Vector3(0,-1,0) yawForce sensitivity; }

}

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

1 Reply

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

Answer by BinaryCaveman · Aug 31, 2010 at 07:07 PM

You could check the Input.mousePosition every frame (in the Update() function), store it in a variable, and if it differs from the previous frame, alter the thrust/roll accordingly. (This would be the equivalent of WASD)

private var previousMousePosition : Vector3; // Where you store the previous mouse position

function Update() { if (Input.mousePosition.x - previousMousePosition.x != 0) // Check for horizontal change { SetRoll((Input.mousePosition.x - previousMousePosition.x) 500); } if (Input.mousePosition.y - previousMousePosition.y != ) // Check for vertical change { SetThrust((Input.mousePosition.y - previousMousePosition.y) 300); }

 previousMousePosition = Input.mousePosition; // Set the current mouse position for the next frame

}

This is untested code, so it might not work, but you get the general idea. :)

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

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

Rotating object using mouse movement 1 Answer

Zoom in/out with right button of mouse 1 Answer

GCMouse and GCKeyBoard for Unity Games on IOS 14. Pointer Lock not working 1 Answer

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


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