- Home /
Using GUI.HorizontalSlider on Vector2?
Hello! I am attempting to create a Sensitivity Slider with GUI.HorizontalSlider
by grabbing the values from another script's line: public Vector2 MouseLookSensitivity = new Vector2(5.0f, 5.0f);
I'm getting the error cannot convert 'UnityEngine.Vector2' expression to type 'float'
and I would like some guidance on what to do differently to achieve my goal. :)
Answer by allenallenallen · Jan 12, 2016 at 07:37 AM
The error means exactly what it says.
The variable MouseLookSensitivity is a Vector2: http://docs.unity3d.com/ScriptReference/Vector2.html
On the other hand, GUI.HorizontalSlider is a float: http://docs.unity3d.com/ScriptReference/GUI.HorizontalSlider.html
You can't set a float to a Vector2.
Why exactly do you need the MouseLookSensitivity variable for? It has two floats since it's a Vector2. Do you perhaps want to use them as position values? Or slider values for the GUI.HorizontalSlider? Look at the parameters for GUI.HorizontalSlider closely.
I want to use the two floats from $$anonymous$$ouseLookSensitivity as slider values for the GUI.HorizontalSlider. Similar to as if I was trying to use a GUI.HorizontalSlider to change the Rotation Speed x and y values from the $$anonymous$$obile First Person Control script.
Is there a different method to doing this or some sort of conversion?
Well, obviously you need two different sliders to get the X and Y mouse sensitivity.
Something like this should work. To get the X and Y values of a Vector2, simple do Vector2.x and Vector2.y.
public float x$$anonymous$$ouseSlider;
public float y$$anonymous$$ouseSlider;
void OnGUI() {
x$$anonymous$$ouseSlider = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), $$anonymous$$ouseLookSensitivity.x, 0.0F, 10.0F);
y$$anonymous$$ouseSlider = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), $$anonymous$$ouseLookSensitivity.y, 0.0F, 10.0F);
}
Answer by Eno-Khaon · Jan 12, 2016 at 11:10 AM
As an alternative approach, if you intend to have a single "sensitivity" scale for both X and Y axes, you can use a float to maintain your entire sensitivity and multiply the Vector2 by that value.
float mouseLookSensitivity;
Vector2 mouseScale;
void OnGUI()
{
mouseLookSensitivity = GUI.HorizontalSlider(**DETAILS HERE**);
mouseScale = Vector2.one * mouseLookSensitivity; // Vector2.one used under the assumption of equivalency
}
mouseScale may not even be necessary, depending on implementation. Alternatively, mouseScale could also be given two separate values to provide a different degree of sensitivity to each axis.