1

I get an issue with my camera when I move my player around in a level. What I mean by this is: When looking around using the mouse, the game runs smoothly, however once I start to use WASD to move my player around, the camera jitters.

Here is a video of just that (might be hard to percieve)

My camera script and the the script that makes the camera follow the player are two different scripts and the issue only occurs when the camera has to follow the player, I thus assumed that the issue was from the MovePlayerCam script which goes as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovePlayerCam : MonoBehaviour
{
    public Transform CamHolder;

    void Update()
    {
        transform.position = CamHolder.position;
        // camHolder is an empty GameObject that is contained by a player Parent Object that has the player movement script
    }
}

I have another project of mine, with the same MovePlayerCam script, in which the issue doesn't occur. However, in this project I am using the old unity input system to move my player around. In the project I'm having the issue with, I am trying to learn how to use the new Input system, I might've broken something there so here is my player movement script as well:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using UnityEngine;

public class BaseMovement : MonoBehaviour
{
    [Header("ref")]
    Rigidbody rb;
    public Transform orientation;
    public PlayerInputs playerControls;

    [Header("Values")]
    public float moveSpeed;
    public float jumpStrength;
    public float maxMoveSpeed;
    public float airMultiplier;
    public float groundDrag;
    public float airDrag;
    public float crouchDrag;
    public float slideDrag;
    bool grounded;


    Vector3 moveDirection;
    private InputAction move;
    private InputAction vertical;

    private void Awake()
    {
        playerControls = new PlayerInputs();
    }

    private void OnEnable()
    {
        move = playerControls.Player.Move;
        move.Enable();
    }
    private void OnDisable()
    {
        move.Disable();
    }

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.drag = groundDrag;
    }

    void Update()
    {
        MoveInputs();
    }

    private void MoveInputs()
    {
        if (rb.velocity.magnitude < maxMoveSpeed)
        {
            moveDirection = orientation.forward * move.ReadValue<Vector2>().y + orientation.right * move.ReadValue<Vector2>().x;
            rb.AddForce(moveDirection.normalized * moveSpeed * airMultiplier, ForceMode.Force);
        }
    }
}

To Fix this, I tried putting the MovePlayerCam script in lateUpdate, in a FixedUpdate and none worked. I still get the camera jitter when moving around. So please help lmao

notPatern
  • 42
  • 7
  • 2
    You are doing physics work inside of Update. Try calling `MoveInputs` in `FixedUpdate`. – hijinxbassist May 03 '23 at 00:35
  • It didn't fix anything, Exept that I had to readjust some variables to make the player move at a decent speed, the camera still jitters :/ – notPatern May 03 '23 at 00:39
  • 1
    Did you try changing the camera movement to `FixedUpdate` as well? – hijinxbassist May 03 '23 at 00:51
  • 1
    as mentioned in the post, changing the update didn't work – notPatern May 03 '23 at 01:22
  • 1
    You are moving the camera by adding a force every frame, this not a framerate indepenant way of doing it. You have two choices, manually specify the velocity in some stable way (only modifying it non-framerate-dependantly). This will lose pure physics simulation. or ease your camera movement by lerping it towards the position each frame – Jay May 03 '23 at 02:31
  • 1
    @rshepp op is correct, late update or regular update will make no difference to this problem, it has nothing to do with execution order – Jay May 03 '23 at 02:31
  • 1
    @reshep I don't mean to be pedantic but this rule of thumb is a poor one. You should move your camera in relation to the place where you move your camera's target - this will minimise jitter. Say for you are tweening based on movement in fixed update, using late update will either miss movement, or apply tweening function more than once. I don't say this without authority. [Cinemachine provides both options exactly for this reason](https://github.com/svermeulen/CebuGameJam/blob/master/UnityProject/Assets/Template/Add-Ons/Cinemachine/Base/Runtime/Behaviours/CinemachineBrain.cs) – Jay May 03 '23 at 03:07

0 Answers0