- Home /
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; }
 }  
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. :)
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                