Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Camera being choppy

Discussion in 'Scripting' started by Deleted User, Aug 28, 2023.

  1. Deleted User

    Deleted User

    Guest

    Hello!

    My camera is being quite werid, sometimes it work fine but a couple of times when i run or build the game my camera start getting really choppy when using the mouse. The times it works fine it stops when you change settings in the inspector.

    I use the new input system and for the camera i have this setup for the lookAction: Value/Vector2 and mouseDelta as the keybinding.

    The camera is a child of the player!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class CameraController : MonoBehaviour
    7. {
    8.     public InputActionAsset playerControls;
    9.     private InputActionMap cameraMap;
    10.     private InputAction lookAction;
    11.  
    12.     public float mouseSensitivity = 10f;
    13.  
    14.     private float xRotation;
    15.  
    16.     public Transform player;
    17.  
    18.     private void Awake()
    19.     {
    20.         Cursor.lockState = CursorLockMode.Locked;
    21.         Cursor.visible = false;
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         Vector2 mouseDelta = lookAction.ReadValue<Vector2>();
    27.         float mouseX = mouseDelta.x * mouseSensitivity * Time.deltaTime;
    28.         float mouseY = mouseDelta.y * mouseSensitivity * Time.deltaTime;
    29.  
    30.         xRotation -= mouseY;
    31.         xRotation = Mathf.Clamp(xRotation, -80, 85);
    32.  
    33.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    34.         player.Rotate(Vector3.up * mouseX);
    35.     }  
    36.  
    37.     private void OnEnable()
    38.     {
    39.         cameraMap = playerControls.FindActionMap("CameraMap");
    40.         lookAction = cameraMap.FindAction("LookAction");
    41.  
    42.         cameraMap.Enable();
    43.     }
    44.  
    45.     private void OnDisable()
    46.     {
    47.         cameraMap.Disable();
    48.     }
    49. }
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    this makes no sense, you make xRotation constantly decrement by the y value. Are you missing a yRotation?
     
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Ohh, I see what you're doing, you're inverting the y, for the x axis rotation of up/down.. gotcha
     
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I found an old Character Controller script, for player move:
    Code (CSharp):
    1. moveLR = Input.GetAxis("Horizontal") * speed;
    2. moveFB = Input.GetAxis("Vertical") * speed;  
    3.  
    4. rotX = Input.GetAxis("Mouse X") * sensitivity;
    5. rotY = Input.GetAxis("Mouse Y") * sensitivity;
    6.  
    7. Vector3 movement = new Vector3 (moveLR, 0, moveFB);
    8. transform.Rotate (0, rotX, 0);                
    9. eyes.transform.Rotate (rotY, 0, 0); //To invert up/down (-rotY, 0, 0)
    10. movement = transform.rotation * movement;
    11. playerCharController.Move(movement * Time.deltaTime);
    Maybe try this ^ style, and see if it helps. :)
     
  5. Deleted User

    Deleted User

    Guest

    Thank you a´lot for the response i tríed your code and it worked perfect i dont get the werid choppy movement anymore.
     
    wideeyenow_unity likes this.