- Home /
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.
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.
Thanks for doing this. Really helps those of us from the future googling around :)
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);
}
}
Your answer
Follow this Question
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