Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Joystick Camera Help

Discussion in 'Scripting' started by JacksonTheXtremeGamer, Mar 18, 2020.

  1. JacksonTheXtremeGamer


    Jun 15, 2019
    I have the controller and keyboard setup all ready to go, but for some odd reason the camera just wanders all around, even when I'm not using the right stick. Here is the script:
    Code (CSharp):
    1. public class MouseCamera : MonoBehaviour
    2. {
    3.     public float MouseRotationSpeed;
    4.     public float JoystickRotationSpeed;
    5.     public Transform Target, Player;
    6.     float mouseX, mouseY;
    7.     public float currentRotation;
    9.     void Start()
    10.     {
    11.         Cursor.visible = false;
    12.         Cursor.lockState = CursorLockMode.Locked;
    13.         mouseX = currentRotation;
    14.     }
    16.     void LateUpdate()
    17.     {
    18.         CamControl();
    19.     }
    21.     public void CamControl()
    22.     {
    23.         mouseX += Input.GetAxis("Mouse X") * MouseRotationSpeed;
    24.         mouseY -= Input.GetAxis("Mouse Y") * MouseRotationSpeed;
    25.         mouseX += Input.GetAxis("Controller X") * JoystickRotationSpeed;
    26.         mouseY -= Input.GetAxis("Controller Y") * JoystickRotationSpeed;
    27.         mouseY = Mathf.Clamp(mouseY, -60, 10);
    29.         transform.LookAt(Target);
    31.         Target.rotation = Quaternion.Euler(mouseY, mouseX, 0);
    32.         Player.rotation = Quaternion.Euler(0, mouseX, 0);
    33.     }
    Is there a way to make the camera lock in place when the right stick is not in use? Any help is appreciated.
  2. elliotfriesen


    Mar 17, 2020
    This is called a dead zone. Controller dead zone is how much the stick has to be pushed to be registered. If you are just continuously taking input then the camera will move because the joystick is not exactly centered to go around this you can say some thing like if(input.getaxis(“controller y”) > 0.5) then...
    After this you can list the movement you want your camera to do. This allows it to only take input when the joystick is half way presses and not just a tiny bit also you would have to do this for the x axis
  3. JacksonTheXtremeGamer


    Jun 15, 2019
    Or just adjust the dead zone value from the input settings. I input .01 as the value, and it works like a charm. Maybe I should make a tutorial on this just to prevent other people from asking the same question. Especially if they plan to make a first-person game. I appreciate the help though. Thanks a bunch.