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
0
Question by uplusion23 · Sep 01, 2014 at 10:48 AM · inputmouseaxisspace

Multiple Constant Force Inputs at once?

I have some modified code from a post on here, by a user named OoglyWoogly I believe, and I have added input for the mouse and keyboard, the keyboard as a backup. When moving the mouse along an axis, x or y, it only uses one input at a time, and I believe it's due to how constant force works. Would anyone be able to assist me, so that the object, being a spacecraft, will pitch/yaw at the same time depending on the mouse location?

 private var previousMousePosition : Vector3;
 
 var LThruster : ParticleSystem;   
 var RThruster : ParticleSystem;   
 var pilot : Rigidbody;
 var invertPitch : boolean = true;
 var invertYaw : boolean = false;
 var sensitivity : float = 1.0;
  
 function Start () 
 {
     //LThruster = GetComponentsInChildren.tag("LThruster");
     LThruster.Stop(true);
     //RThruster = transform.Find("RThruster");
     RThruster.Stop(true);
     rigidbody.AddForce(0,0,0);
 }
 function Update () {
     // Did the user press fire?
     //if (Input.GetButton ("Fire1"))
      //   BroadcastMessage("Fire");
  
     if (Input.GetKeyDown("w"))
     {
         SetThrust(600);
         LThruster.Play(true);
         RThruster.Play(true);
  
     }
  
     if (Input.GetKeyUp("w"))
     {
         SetThrust(0);
         LThruster.Stop(true);
         RThruster.Stop(true);
  
     }
  
     if (Input.GetKeyDown("s"))
     {
         SetThrust(-500);
  
     }
  
     if (Input.GetKeyUp("s"))
     {
         SetThrust(0);
         LThruster.Stop(true);
         RThruster.Stop(true);
     }
  
     if (Input.GetKeyDown("q"))
     {
         SetRoll(400);    
  
     }
  
     if (Input.GetKeyUp("q"))
     {
         SetRoll(0);
  
     }
  
     if (Input.GetKeyDown("e"))
     {
         SetRoll(-400);   
  
     }
  
     if (Input.GetKeyUp("e"))
     {
         SetRoll(0);
  
     }
  
         if (Input.GetKeyDown("i"))
     {
         SetPitch(375);
  
     }
  
     if (Input.GetKeyUp("i"))
     {
         SetPitch(0);
  
     }
  
     if (Input.GetKeyDown("k"))
     {
         SetPitch(-375);  
  
     }
  
     if (Input.GetKeyUp("k"))
     {
         SetPitch(0);
  
     }
  
         if (Input.GetKeyDown("j"))
     {
         SetYaw(355);  
  
     }
  
     if (Input.GetKeyUp("j"))
     {
         SetYaw(0);
  
     }
  
     if (Input.GetKeyDown("l"))
     {
         SetYaw(-355); 
  
     }
  
     if (Input.GetKeyUp("l"))
     {
         SetYaw(0);
  
     }
     
     if (Input.GetKeyDown("space"))
     {
         ThrustUp(300);
     }
  
     if (Input.GetKeyUp("space"))
     {
         ThrustUp(0);
     }
     
     if (Input.GetKeyDown(KeyCode.LeftControl))
     {
         ThrustUp(-300);
     }
  
     if (Input.GetKeyUp(KeyCode.LeftControl))
     {
         ThrustUp(0);
     }
     //Testing for mouse
     if (Input.GetAxis("Mouse Y"))
     {
         SetPitch((Input.mousePosition.y - (Screen.height / 2)) * 2);
         Debug.Log((Input.mousePosition.y - (Screen.height / 2)) * 2);//debug
     }
     if (Input.GetAxis("Mouse X"))
     {
         SetYaw((Input.mousePosition.x - (Screen.width / 2)) * 2);
         Debug.Log((Input.mousePosition.x - (Screen.width / 2)) * 2);//debug
     }
 }
  
 function SetThrust ( thrustForce : int)
     {
         pilot.constantForce.relativeForce = Vector3.forward * thrustForce * sensitivity;
     }
 
 function ThrustUp ( thrustForce : int)
     {
         pilot.constantForce.relativeForce = Vector3.up * 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;
         }
  
 }


Quick Edit: I've noticed that it doesn't happen only the mouse based axis, but instead, any multiple axis edit

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
0

Answer by kariwmklawm · Sep 01, 2014 at 12:28 PM

It's because both SetYaw and SetPitch are hard-setting the Vector3. So the Y axis of the mouse sets the pitch, then you set the X axis to the yaw, ending up with only one of the two inputs you want. If you want to still hard set the constant force values for individual axis, you'll want something like this:

     function SetPitch(pitchForce : int)
     {
         if (invertPitch)
         {
             pilot.constantForce.relativeTorque =
                 Vector3(pitchForce * sensitivity,
                 pilot.constantForce.relativeTorque.y,
                 pilot.constantForce.relativeTorque.z);
         }
         else
         {
             pilot.constantForce.relativeTorque = 
                 Vector3(-pitchForce * sensitivity, 
                 pilot.constantForce.relativeTorque.y,
                 pilot.constantForce.relativeTorque.z);
         }
     }
 
     function SetYaw(yawForce : int)
     {
         if (invertYaw)
         {
             pilot.constantForce.relativeTorque = 
                 Vector3(pilot.constantForce.relativeTorque.x, 
                     yawForce * sensitivity,
                     pilot.constantForce.relativeTorque.z);
         }
         else
         {
             pilot.constantForce.relativeTorque = 
                 Vector3(pilot.constantForce.relativeTorque.x, 
                     -yawForce * sensitivity, 
                     pilot.constantForce.relativeTorque.z);
         }
 
     }
 
Comment
Add comment · Show 1 · 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 uplusion23 · Sep 01, 2014 at 02:08 PM 0
Share

I'd like to upvote you but I can't. It seems to a work a hundred times better, thanks! I just started Javascript a bit ago, two weeks I'd say. Now I just need to work on multiple keyboard inputs. Same concept right?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Input Axis Mouse ScrollWheel 1 Answer

Custom mouse cursor is laggy on low/mid hardware 0 Answers

Mouse input erratic 1 Answer

Raycast based on a Rect? 1 Answer

Mouse axis wont change an integar. 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