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 phosfiend · Jan 08, 2011 at 08:51 PM · mouseaxismouselooky-axisinvert

Toggle the Y axis Invert in MouseLook.cs with a keystroke?

Howdy,

I'm a javascript guy, and I can't seem to figure out how to implement toggle to flip the Y axis sensitivity (aka invert mouse) in MouseLook.cs. I would like for the user to be able to hit the "I" key and toggle the Y-axis.

Thanks!

Here's the MouseLook.cs code:

using UnityEngine; using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Look")] public class MouseLook : MonoBehaviour {

 public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
 public RotationAxes axes = RotationAxes.MouseXAndY;
 public float sensitivityX = 15F;
 public float sensitivityY = 15F;

 public float minimumX = -360F;
 public float maximumX = 360F;

 public float minimumY = -20F;
 public float maximumY = 20F;

 float rotationX = 0F;
 float rotationY = 0F;


 Quaternion originalRotation;

 void Update ()
 {

     if (axes == RotationAxes.MouseXAndY)
     {
         // Read the mouse input axis
         rotationX += Input.GetAxis("Mouse X") * sensitivityX;
         rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

         rotationX = ClampAngle (rotationX, minimumX, maximumX);
         rotationY = ClampAngle (rotationY, minimumY, maximumY);

         Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
         Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);

         transform.localRotation = originalRotation * xQuaternion * yQuaternion;
     }
     else if (axes == RotationAxes.MouseX)
     {
         rotationX += Input.GetAxis("Mouse X") * sensitivityX;
         rotationX = ClampAngle (rotationX, minimumX, maximumX);

         Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
         transform.localRotation = originalRotation * xQuaternion;
     }
     else
     {
         rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
         rotationY = ClampAngle (rotationY, minimumY, maximumY);

         Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
         transform.localRotation = originalRotation * yQuaternion;
     }
 }

 void Start ()
 {
     // Make the rigid body not change rotation
     if (rigidbody)
         rigidbody.freezeRotation = true;
     originalRotation = transform.localRotation;
 }

 public static float ClampAngle (float angle, float min, float max)
 {
     if (angle < -360F)
         angle += 360F;
     if (angle > 360F)
         angle -= 360F;
     return Mathf.Clamp (angle, min, max);
 }

}

Comment
Add comment · Show 2
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 Jesse Anders · Jan 08, 2011 at 09:11 PM 0
Share

What do you mean by 'y sensitivity invert'?

avatar image phosfiend · Jan 08, 2011 at 09:26 PM 0
Share

An invert Y toggle - sorry if my nomenclature is a little obtuse. Very common in FPS's.

3 Replies

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

Answer by Jesse Anders · Jan 08, 2011 at 09:54 PM

Does this:

Input.GetAxis("Mouse Y")

Appear somewhere in the script?

If so, I imagine all you'd need to do is negate that value when the 'invert' option is activated.

There's a few different ways you could implement this. One would be to store a multiplier as a member variable, initialized to 1 or -1 depending on whether the 'invert' option is on initially. You can then use Input.GetKeyDown() or GetButtonDown() to detect when the 'invert mouse y' button is detected, and negate the multiplier. (This can of course also be done in response a GUI event, etc.).

Then, simply multiply the return value of Input.GetAxis("Mouse Y") by the multiplier.

Comment
Add comment · Show 6 · 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 phosfiend · Jan 08, 2011 at 10:20 PM 0
Share

I've updated the above with the respective code. Sorry for all the hand-holding, I'm still very much in the learning stages here.

avatar image Jesse Anders · Jan 08, 2011 at 10:45 PM 0
Share

Does that mean you still need help? (Did you try the idea I suggested?)

avatar image phosfiend · Jan 08, 2011 at 11:00 PM 0
Share

Thank you very much for your time already, but yes, I'm afraid I do still a bit of help! The C# code is different enough that I'm unsure how to implement your suggestion. If it were JS I think I could pull it off myself.

Thanks!

avatar image Jesse Anders · Jan 08, 2011 at 11:15 PM 0
Share

The C# version isn't going to be that different from the UnityScript version. First, you need a 'y$$anonymous$$ultiplier' variable (which you can of course call whatever you want). The syntax for declaring variables is different in C# than in US, but since you have the script right there, you can see what the syntax looks like (look at the declaration of 'rotationX' for an example). The rest of the code will be basically the same (or maybe exactly the same) in both languages. You should be able to find all the info you need in my answer above, but let me know if you have further questions.

avatar image phosfiend · Jan 08, 2011 at 11:21 PM 0
Share

I'll give it a try! Thanks again

Show more comments
avatar image
1

Answer by superdupergc · Sep 15, 2013 at 06:08 PM

Easiest way to do this is to negate sensitivityY in MouseLook.cs.

 sensitivityY = -15F; //for instance
 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
 
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
avatar image
0

Answer by The_r0nin · Jan 08, 2011 at 09:53 PM

Well, if the input manager won't let you select that at runtime (I honestly don't know), then you just create a custom cursor for your gameplay and an "invertMouse" boolean variable:

if (!invertMouse){
    cursorPos.y = Screen.height - mousePosition;
}
else{
    cursorPos.y = mousePosition;
}

I think that the mouse Y position is already inverted with respect to the GUI coordinate system, which is why it looks backwards in the code.

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 The_r0nin · Jan 08, 2011 at 09:55 PM 0
Share

And yes, I noticed that you wanted to recode the mouse.cs... but why go through all of that (unless you are happy with the default system mouse icon)? This will give you much more flexibility later (depending on your game requirements)...

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

1 Person is following this question.

avatar image

Related Questions

How do I invert an input axis using Javascript? 1 Answer

Make the gameobject flip to the direction it is being dragged to? 1 Answer

How do I determine mouse speed on the x and z axis? 0 Answers

Unity Fog Script for a Certain level 1 Answer

Stop object rotation 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