Hey all,
I am brand new to unity and been running through some tutorials while on the side putting together a script for movement of spaceship. So far I have been able to get most of this down in C# but I cannot seem to wrap my head around Input.GetAxis(“MouseScrollWheel”) and exactly how it functions.
My end goal is to have mouse scroll wheel up adds to speed by 1 until it caps at 10 and then mouse wheel down drops speed back down by 1 until it returns to 0. I guess I am having a hard time understanding the mouse wheel without having a defined positive and negative buttons.
Any help in understanding this input function would be great!
it returns the sensitivity value in input settings when you scroll, negative value when scrolling down, positive when up, at least for me
so to change a value by increments of 1 you could for example set the sensitivity to 1 and use
int n = 0;
void Update()
{
n += Mathf.RoundToInt(Input.GetAxis("Mouse ScrollWheel"));
n = Mathf.Clamp(n, 0, 10);//prevents value from exceeding specified range
Debug.Log(n);
}
you could also use float instead of int which is what GetAxis returns but if you need just integers might as well convert it to int to avoid float inaccuracy in case you want to use the value for stuff where that becomes a problem. for doing that Mathf.RoundToInt function is just one way out of many, it works here since it rounds to nearest integer. theres also GetAxisRaw but not sure how that works
version without converting to int:
float n = 0;
void Update()
{
n += Input.GetAxis("Mouse ScrollWheel");
n = Mathf.Clamp(n, 0, 10);//prevents value from exceeding specified range
Debug.Log(n);
}
1 Like
Awesome, thanks for the reply. That definitely helps me in understanding it and will give me something to start with when i’m off work. Thanks!