• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

New Input System: Event on input type changed (between mouse, keyboard and controller) 0 Answers

2D: How to make Gradius III with a mouse? 1 Answer

How can I use only keyboard in FPC? 0 Answers

Character - Move In Mouse Direction 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges