GetMouseButton reports bad state at startup?

Hi,

My camera is working the way I want it to, but when I start the game, my click-and-drag camera activates for a fraction of a second causing the camera to appear to jump. It seems like the if statement shown below fails to exit this function before the camera is moved.

The only thing I could think of to stop it is to quit the function until a little time passes.

    private void PanDolly()
    {
        // Quit if the left mouse button isn't held down
        if (!Input.GetMouseButton(0)) return;

        // This is a bug workaround. If this update runs too soon at the start
        //      of the game, the mousebutton check above will be fooled and 
        //      this script will be active for a moment causing the 
        //      camera to jump unexpectedly.
        if (Time.time < 0.3) return;

        float mouseX = Input.GetAxis("Mouse X");
        float mouseY = Input.GetAxis("Mouse Y");

        Debug.Log(Time.time + " - " + mouseX + ", " + mouseY);

        // Do camera stuff here....
    }

This version, which includes my workaround, is totally stable. I just hate hacks.

Any ideas?

Are you developing on PC as I had this recently. I think the GetMouseButton functions are actually reading you hitting the Play button in the editor or the Start button on a standalone.

I spent days debugging code until I realised my code was fine and it was this.

I had to use GetMouseButtonDown and Up in the end. I mention PC as I don’t think this is an issue on Mac.

This is what I thought as well.

I hate that I have to put in a Time check which runs on every frame just to protect against something on game startup that shouldn’t be happening.

This is a Unity bug if this is the case. Clicking something in the editor GUI should not affect the game.

By the way, I think something similar happens when you set focus on something in the Editor and then give the focus back to the game. The mouse delta return goes crazy on the first click.