Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Camera Jump Issue

Discussion in 'Scripting' started by FletcherMartin, Aug 14, 2020.

  1. FletcherMartin

    FletcherMartin

    Joined:
    Apr 18, 2019
    Posts:
    2


    Gif link in case it doesn't want to work as an embed: https://imgur.com/3zYCWxM

    I've been trying to get a 3rd person melee action camera together for a while now, but I keep coming across jump/jitter issues. Finally, I go with this ultra simple script and for some reason this random jump issue happens. I don't see any reason for it whatsoever and I'm being driven insane by all the camera issues I've been dealing with. Hoping someone else with some fresh eyes can explain why the camera wants to randomly jump around when it moves.

    Edit: So I figured out that's it has something to do with the project, cause I created a new one, implemented the camera system, no jump issue whatsoever. Unfortunately I'm working in a project that has had quite a few cooks in the kitchen already and is a massive mess, so I have no idea what's causing the issue but yay it's not me being a complete dumb dumb.

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class CameraFollow : MonoBehaviour
    10.  
    11. {
    12.  
    13.  
    14.  
    15. public float CameraMoveSpeed = 120.0f;
    16.  
    17. public GameObject CameraFollowObj;
    18.  
    19. Vector3 FollowPos;
    20.  
    21. public float clampAngle = 80.0f;
    22.  
    23. public float inputSens = 150.0f;
    24.  
    25. public float camDistanceXToPlayer;
    26.  
    27. public float camDistanceYToPlayer;
    28.  
    29. public float camDistanceZXToPlayer;
    30.  
    31. public float mouseX;
    32.  
    33. public float mouseY;
    34.  
    35. public float finalInputX;
    36.  
    37. public float finalInputZ;
    38.  
    39.  
    40.  
    41.  
    42.  
    43. private float rotationY = 0.0f;
    44.  
    45. private float rotationX = 0.0f;
    46.  
    47.  
    48.  
    49. void Start()
    50.  
    51. {
    52.  
    53. Vector3 rot = transform.localRotation.eulerAngles;
    54.  
    55. rotationY = rot.y;
    56.  
    57. rotationX = rot.x;
    58.  
    59. Cursor.lockState = CursorLockMode.Locked;
    60.  
    61. Cursor.visible = false;
    62.  
    63.  
    64.  
    65. }
    66.  
    67.  
    68.  
    69. void Update()
    70.  
    71. {
    72.  
    73. float inputX = Input.GetAxis("RightStickHorizontal");
    74.  
    75. float inputZ = Input.GetAxis("RightStickVertical");
    76.  
    77. mouseX = Input.GetAxis("Mouse X");
    78.  
    79. mouseY = Input.GetAxis("Mouse Y");
    80.  
    81. finalInputX = inputX + mouseX;
    82.  
    83. finalInputZ = inputZ + mouseY;
    84.  
    85.  
    86.  
    87. rotationY += finalInputX * inputSens * Time.deltaTime;
    88.  
    89. rotationX += finalInputZ * inputSens * Time.deltaTime;
    90.  
    91.  
    92.  
    93. rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
    94.  
    95. Quaternion localRotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
    96.  
    97. transform.rotation = localRotation;
    98.  
    99. }
    100.  
    101.  
    102.  
    103. private void LateUpdate()
    104.  
    105. {
    106.  
    107. CameraUpdater();
    108.  
    109.  
    110.  
    111. }
    112.  
    113.  
    114.  
    115. void CameraUpdater()
    116.  
    117. {
    118.  
    119. Transform target = CameraFollowObj.transform;
    120.  
    121.  
    122.  
    123. float step = CameraMoveSpeed * Time.deltaTime;
    124.  
    125. transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    126.  
    127.  
    128.  
    129. }
    130.  
    131. }
    132.  
    133.  
     
    Last edited: Aug 14, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,412
    You should consider breaking your scenes into multiple additively-loaded scenes, rather than one honkin' big scene. Here's some ways I break up my play scene:

    - single loader scene that additively loads these scenes
    - whatever content is required for this level
    - whatever lighting is required for this level
    - UI system for in-game play
    - player controller
    - pause menu scene overlay

    etc. etc. etc.

    This way your scenes are relatively smaller and less likely to have folks stomping on each other. Once you work this way for a while, you'll never again want to all be in one huge scene.
     
  3. FletcherMartin

    FletcherMartin

    Joined:
    Apr 18, 2019
    Posts:
    2
    The scene in question here is literally it's own test scene with a plane, cubes and camera.

    I've since deleted a very old version of Cinemachine that was haphazardly added to the project - i.e. not via the package manager - and the size of the jumps diminished significantly but still occur.