Pitch Yaw via Mouse - works, but...

I’m making my camera controllable. Basically you can pan, zoom and scroll and when holding down the scroll wheel control rotation and tilting (pitch and yaw) while moving the mouse.

The code for all of that I’ve written works almost perfectly but there is one wall I’ve run into and many searches all yield more or less the same code I’ve written.

private float yaw = 0.0f;
private float pitch = 45.0f;

if (Input.GetMouseButton(2))
{
yaw += mouseRotationSpeed * Input.GetAxis("Mouse X");
pitch -= mouseRotationSpeed * Input.GetAxis("Mouse Y");
pitch = Mathf.Clamp(pitch, 0, 90);
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}```

This works great except the rotation and pitch are super fast. The mouseRotationSpeed variable does nothing to slow it down regardless of how small or large I make it. Thinking it was a frame issue I added a
```* Time.deltaTime``` to the yaw and pitch statements. At this point it gets painfully slow. Even when I increase the mouseRotationSpeed to 2 million, there is zero difference in the movement. I would expect a change in this variable to have some kind of affect on how fast or slow the movement happens.

So at this point it's either hyper fast or painfully slow. Any recommendations on how to get something I can actually control/tweak via the mouseRotationSpeed variable?

If you are changing that 20f in code, and you already put the script in the scene before, Unity is now supplying the first value you ever supplied to it from either the scene or prefab.

Try print out the values for mouseRotationSpeed in your Start() method to see this, or make sure you only change it in the scene/prefab.

Or if you want to change it in code, make that variable private.

The usual pattern is to just define the public variable, without a value.

If you want default values, implement the Reset() method:

That plays “nice” in the editor.

This is just a prototype at this point to test out the various “pieces” that will eventually make up the game I’m working on. Setting it public or private really makes no difference though in terms of the issue I’m encountering. For example I made it private, ran code, still hyperfast. Changed the 20f to .02f, or one thousandth of what it was (while still private) and there is zero difference in the performance. Still hyperfast.

On start it’s 20 and sticking a debug in the update loop shows it never changes from 20 (as expected).

Thanks for pointing out the “typo”. I do understand the difference of both, I’ve been coding in well over two dozen languages since the mid 80s, and final code for me is much different from quick and dirty prototype tests.

How long you’ve been coding doesn’t apply here. The code works fine. Ran it myself just now, I can change that rate all day long in the inspector or code and it works fine. Look elsewhere. It’s something in the plumbing. With Unity, the plumbing is equally important to the code. In fact, it’s even more important, IMNSHO.

Make a blank scene, put a cube in it, point a camera at that cube, put the script on there. If it works, start looking for gremlins in the scene where it fails. Enclosed find my scene. Good luck!

6066146–657227–Nuggets.unitypackage (3.43 KB)

OK… two things changed from yesterday to today.

  1. I closed and reopened the project.
  2. Upon reopening it told me I was on 2019.4.0f1 do I want to update to 2019.4.3f1.

I had time to spare so I closed the project went ahead and did the update and then opened the project again. Now the exact same code I had before is working fine and as expected. If I change the values of the mouseRotationSpeed the yaw and pitch changes speed as expected. I did add in the Time.deltaTime again to tamp it down but exactly as I have the code listed above (plus the Time) is what is working just fine now.

I still appreciate your advise on ensure variables, constants and such are setup properly but in this case the whole issue is possibly a bug (since i changed versions and it went away) or some internal glitch that simple closing and reopening the project fixed.