Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheGreatS · Nov 09, 2015 at 01:02 PM · c#unity 5mouselookfirst-person-controllersensitivity

Unity 5 access First Person Controller Mouse Look Sensitivity via script

I am currently working with the default FPSController in unity, and I have a child of the First Person Character object that contains a script I am using to control mouse sensitivity. When I look in the inspector I can see the Mouse Look in the First Person Controller Script Component with X and Y Sensitivity, but when I try to access them via

 m_FirstPersonController = transform.parent.GetComponent<FirstPersonController> ();
 XSensitivity = m_FirstPersonController.XSensitivity;

or

 m_MouseLook = transform.parent.GetComponents<MouseLook> ();
 XSensitivity = m_MouseLook.XSensitivity;

I get a NullReferenceException on the variable declaration line. My guess is this is because MouseLook is another script not actually attached to the object but is attached in a way to the FirstPersonController script, however I do not know how to access a script within a script so that I can get at the sensitivity variables.

If there is a better way to change the mouse sensitivity I would like to know, but so far it seems the best way to do it is through MouseLook.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by TheGreatS · Nov 12, 2015 at 01:11 AM

Asked the question on reddit and got a responce, so now I'm posting the solution here for posterity.

I have the Child Object and Player Object. The Child Object contains the script that I want to use to access the Mouse Look script and the Player Object contains the script that actually has access to the Mouse Look script.

In the Player Object script I add two functions.

 public float GetMouseSensitivity(bool XorY){
     /*
      * true = x
      * false = y
      */
     if (XorY) {
         return m_MouseLook.XSensitivity;
     } else {
         return m_MouseLook.YSensitivity;
     }
 }

and

 public void ChangeMouseSensitivity(float X, float Y){
     m_MouseLook.XSensitivity = X;
     m_MouseLook.YSensitivity = Y;
 }

To access the mouselook script and get/change the mouse sensitivity.

Then in the child object I get the Player Object script through transform.parent.GetComponent <> (); and use the functions to get and change the mouse sensitivity as needed.

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 JimmyCushnie · Jul 05, 2017 at 10:14 PM 0
Share

Thanks for doing this. Really helps those of us from the future googling around :)

avatar image
1

Answer by sisse008 · Jul 07, 2017 at 10:20 AM

this confued me a bit so to clearify, to access the variables in MouseLook you actually need to write the function described above in the FirstPersonController script (not MouseLook script).

so in FirstPersonController.cs:

 public void limitCameraVerticleRotation(float max_x, float min_x)
 {
             m_MouseLook.MinimumX = min_x;
             m_MouseLook.MaximumX = max_x;
 }


and in your new script:

 //need this header to get reference to FirstPersonController script!!
 using UnityStandardAssets.Characters.FirstPerson;
 
 public class myScript : MonoBehaviour {
          FirstPersonController FPCscript;
 
      void Start(){
            //this is in case the FPSController is the parent of the object holding this script.
             FPCscript = transform.parent.GetComponent<FirstPersonController>();
        }
 
        void Update(){
               FPCscript.limitCameraVerticleRotation(maxX, minX);
         }
 
 }
           

 

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 Hybris_Team · Sep 11, 2018 at 06:01 PM

Thanks this was very helpful!

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# How do I toggle MouseLook in game? 1 Answer

How to use PlayerPrefs to save to floats (Sensitivity Help) 0 Answers

C# on/off any Script Component (mixed C# and JS) 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