Consistent raw mouse input across browsers in unity

I’m using this code to make my FPS character look around (Unity WebGL build):

rotationX += Input.GetAxisRaw("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
tRotation =  originalRotation * xQuaternion;
transform.localRotation= tRotation;

rotationY += Input.GetAxisRaw("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Vector3 tempVector = new Vector3(-rotationY,transform.localEulerAngles.y,transform.localEulerAngles.z);
transform.localEulerAngles = tempVector;

float ClampAngle(float angle,float min, float max)
{
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp (angle, min, max);
}

While this works, I notice it’s not consistent. If I count the amount of Mouse Counts to do a horizontal 360 spin on chrome, and execute the same amount of Mouse Counts in Firefox (and Edge), it does a bit more than a 360 spin. I also notice a difference in Stand-alone builds.

Does anyone know how to get pure mouse input, which is consistent across all browsers? No mouse acceleration, no smoothing, … just plain raw mouse input.

You might be better using Input.mousePosition, converting it to a viewport/world position(Converting Mouse Position to World. Stationary Camera. - Questions & Answers - Unity Discussions), and then recording the difference yourself (between frames). That should be consistent across all platforms etc…

Thanks for the suggestion. Any chance you could help me how to do that?

You’d probably do something like this: (I’m not at my normal dev pc so haven’t tried this)

Vector3 LastPosition;

void Update()
{
  Vector3 CurrentPosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);
  Vector3 DeltaVec = CurrentPosition - LastPosition;
  LastPosition = CurrentPosition;
 
  // Multiply DeltaVec by a value to make it more sensitive
  DeltaVec *= 50.0f;
 
  // Wherever you were using 'Input.GetAxisRaw("Mouse X")' in your code you can now
  // use DeltaVec.x
 
  // Wherever you were using 'Input.GetAxisRaw("Mouse Y")' in your code you can now
  // use DeltaVec.y
 
 
}

Thanks, this makes sense and I understand it better now. But will this still work with a locked cursor? I lock the cursor so it won’t move on full screen mode.

Ah, I’m not sure about that - it would probably just return you the same value each time in that case.

The code you originally had should work though, I’m not sure why it doesn’t as the GetAxisRaw should return the same value on each browser. Perhaps the game is just running at a different speed in each browser and that’s why you’re getting different results. You could try running the code in FixedUpdate rather than Update perhaps?

I capped the game to 60 fps and both browsers have no trouble reaching that target, so I would think they run at the same speed…

Did some testing: the values GetAxisRaw returns are not just -1, 0 or 1. It seems like for mouse input, there’s not difference between GetAxis and GetAxisRaw. Could this be causing the issue?

I was going to mention that. I recently tried using Raw in some code and noticed the same thing.
I’d say try: Time.deltaTime * amountPerSecond * GetAxis and call it a day. :slight_smile:

You could create your own ‘Raw’ value, not sure if that’s helpful or not.