Search Unity

Question fixedupdate more jittery than update

Discussion in 'Scripting' started by maxpanic, Mar 13, 2022.

  1. maxpanic

    maxpanic

    Joined:
    Dec 18, 2021
    Posts:
    5
    hi
    so i'm using the charactercontrolller component. when i put the movement script in update it's perfectly smooth, but in fixedupdate it's more jittery. It's not much but it's noticeable.
    I think it's to do with time.deltatime, which i don't know much about (I do change it to fixeddeltatime when i put it in fixed update though)
    How do i fix this
    thank you

    Code (CSharp):
    1. public class Controller : MonoBehaviour
    2. {
    3.     [SerializeField] [Range(1, 10)] private float speed;
    4.     private CharacterController controller;
    5.  
    6.     private void Awake() {
    7.         controller = GetComponent<CharacterController>();
    8.     }
    9.  
    10.     private void Update() {
    11.        
    12.     }
    13.  
    14.     private void FixedUpdate() {
    15.         Vector2 move = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    16.         controller.Move(move * Time.fixedDeltaTime * speed);
    17.     }
    18. }
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    You must be experiencing this jittery movement from some point of view. This movement is probably for the player, but what is your game view? First person? Third person? Side Scroller? How is your camera being manipulated?

    Just as some general info:

    Update occurs every frame, while FixedUpdate fires off every fixed time step. Since a fixed time step is longer than a frame, there are more frames than there are fixed time steps in any given time. The more frames there are, the smoother something will look like it is moving, just like conventional animation. This however does not entirely explain why you are observed, obvious jittery movement, we will need the the info I am asking about above.

    FixedUpdate is for physics, basically anything related to Rigidbody. You are however using CharacterController, which isn't involved with physics directly. You should not be using fixed update for what you have presented in your code. What are you attempting that led you to do this?
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    And you didn't expect this?

    Update is running at your frame-rate. FixedUpdate is running at a fixed rate (hence the name) which is 50hz by default (you can set this fixed time-step in the Time Manager here). Note that whilst physics is (by default) driven by the PlayerLoop fixed-update, it's not solely "for" physics, lots of other things are driven by this too. Often it's stated it controlled-by or is just for physics but this isn't true. It's true that physics relies on this by default for fixed updates though.