How to add headbobbing to my character in my game?

This is my code, just not sure how to add it on to the back end of it.

using System;
using System.Diagnostics.Contracts;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;

public class Player : MonoBehaviour
    [SerializeField] float mouseSensitivity = 3f;
    [SerializeField] float movementSpeed = 5f;
    [SerializeField] float mass = 1f;
    [SerializeField] float acceleration = 20f;
    public Transform cameraTransform;

    public Vector3 moveDirection =;
    Vector2 rotation =;

    public float Height
        get => controller.height;
        set => controller.height = value;

    public event Action OnBeforeMove;

    internal float movementSpeedMultiplier;

    CharacterController controller;
    internal Vector3 velocity;
    Vector2 look;

    PlayerInput playerInput;
    InputAction moveAction;
    InputAction lookAction;
    InputAction sprintAction;

    void Awake()
        controller = GetComponent<CharacterController>();
        playerInput = GetComponent<PlayerInput>();
        moveAction = playerInput.actions["move"];
        lookAction = playerInput.actions["look"];
        sprintAction = playerInput.actions["sprint"];
    void Start()
        Cursor.lockState = CursorLockMode.Locked;

    // Update is called once per frame
    void Update()

    void UpdateGravity()
        var gravity = Physics.gravity * mass * Time.deltaTime;
        //determines the vertical velocity
        velocity.y = controller.isGrounded ? -1f : velocity.y + gravity.y;

    Vector3 GetMovementInput()
        //takes horizontal and vertical input from the keyboard
        //var x = Input.GetAxis("Horizontal");
        var y = Input.GetAxis("Vertical");

        var moveInput = moveAction.ReadValue<Vector2>();

        var input = new Vector3();
        //input vector by adding the vector pointing from the player forwards, multiplied by 'y'
        //'w' and 's' keys
        input += transform.forward * moveInput.y;
        //input vector by adding the vector pointing from the player, to the right, multiplied by 'x'
        //'a' and 'd' keys
        input += transform.right * moveInput.x;
        //prevents the player from moving faster when moving diagonally 
        input = Vector3.ClampMagnitude(input, 1f);
        input *= movementSpeed * movementSpeedMultiplier;
        return input;
    void UpdateMovement()
        movementSpeedMultiplier = 1f;

        var input = GetMovementInput();

        var factor = acceleration * Time.deltaTime;
        velocity.x = Mathf.Lerp(velocity.x, input.x, factor);
        velocity.z = Mathf.Lerp(velocity.z, input.z, factor);

        controller.Move(velocity * Time.deltaTime);
    void UpdateLook()
        var lookInput = lookAction.ReadValue<Vector2>();
        look.x += lookInput.x * mouseSensitivity;
        look.y += lookInput.y * mouseSensitivity;

        look.y = Mathf.Clamp(look.y, -89f, 89f);

        //rotates the player object
        transform.localRotation = Quaternion.Euler(0, look.x, 0);
        //cannot do the same for the 'y' rotation as we need to angle the player object itself
        cameraTransform.localRotation = Quaternion.Euler(-look.y, 0, 0);


Hey Scryllix!

My honest opinion: Don’t, almost nobody likes it haha.

If you still insist of implementing a head bob, you could use the Cinemachine’s built-in functions to simulate camera shake etc.