Search code examples
c#unity-game-enginecamera

How to use either gamepad joystick input or mouse input for camera turning? (NO CINEMACHINE and using old input system)


As the title suggests, I am not using cinemachine and have coded my own orbiting camera script. It works great when the input axes detect only the mouse, but as soon as I add in detection for the right joystick on my controller it goes all wonky. The camera moves continuously to the left unless the stick is pushed to the right, but even in dead zone it will continuously move to the left. Everything else works perfect on controller, just not camera movement.

Here's what my camera movement script looks like:

transform.LookAt(target); //make sure we're looking at the target. TODO: make it lag a little bit!
    if (canControlCam)  //may i?
    {
        mouseX += Input.GetAxis("Mouse X") * rotSpeed;  //get the x and y values of the mouse, rotSpeed is sensitivity. TODO: change this to work with either mouse or gamepad
        mouseY -= Input.GetAxis("Mouse Y") * rotSpeed;  
        mouseY = Mathf.Clamp(mouseY, -60, 50);  //clamp the mouse so it doesn't freak out. Update: it's freaking out anyway. Could this be the issue?
        

        if (!moving)    //if we are stationary, allow movement around the player.
        {
            target.rotation = Quaternion.Euler(mouseY, mouseX, 0);
        }
        else //if we're moving, make the player rotate with the camera. Feels much more natural
        {
            target.rotation = Quaternion.Euler(mouseY, mouseX, 0);
            player.rotation = Quaternion.Lerp(player.rotation, Quaternion.Euler(0, mouseX, 0), time);
            //player.rotation = Quaternion.Euler(0, mouseX, 0);
        }
    }

As you can see it's very simple. Now, I have the "Mouse X" and "Mouse Y" axes implemented twice like I do for the other inputs: one for MnK, one for gamepad. Neither work correctly.

Here's what the inputs for Mouse Y look like in the project manager:

Mouse input for Mouse Y Controller input for Mouse Y

I am genuinely confused. Any advice as to how to get this to stop acting funky, that would be wonderful. I cannot switch to cinemachine and cannot switch to the new input system, as I have less than a day to get this working before I present! It's no big deal if I can't get it to work, but I would really love it as the game feels silky smooth otherwise on controller.

Things I have tried: turning off transform.LookAt(), taking off the + and - to mouseX and mouseY, changing the target.rotation to target.Rotate() instead of eulers, changing gravity dead zone and sensitivity inside the input manager. None of these have worked.


Solution

  • Update: I found the solution. Comparing the two horizontal and vertical axes that unity gives you as a baseline, I found that the dead zone was in fact the issue. Setting the deadzone to .19 with the sensitivity to 1 and gravity to 0 for the gamepad Mouse X and Mouse Y solved the issue.