Changing FOV with Speed C#

Hey, I recently got back into coding after a while as apparent by all the comments. In this movement script everything works fine, but I am trying to find a way to correlate the field of view with the speed of the player. That also means that I am trying to add a gradual acceleration when sprinting instead of the instant transition. I feel I could add the gradual acceleration if given a way to track the true movement speed of the player, as the “speed” value does not change when sprinting. Anything helps, thanks.

public CharacterController controller;

//General values
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public float sprintMultiplier = 2;

//Ground Check
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;

Vector3 velocity;
bool isGrounded;

//booligans
public bool isSprinting;

//camera
public Camera playerCam;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;

    //More ground check, heavy & can't jump if you dont write what the ground is :)
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if(isGrounded && velocity.y < 0)
    {
        velocity.y = -2f;
    }

    //Jump function
    if(Input.GetButtonDown("Jump") && isGrounded)
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    }

    //Sprint function
    if(Input.GetKey(KeyCode.LeftShift))
    {
        isSprinting = true;
    }
    else
    {
        isSprinting = false;
    }

    //Checks if you're pressing WASD
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    //Changes the value of your position/"move value" corresponding to your WASD input
    Vector3 move = transform.right * x + transform.forward * z;

    //important to put all "If True" checks after this^ as the game will get confused if you try to edit a value that doesn't exist yet but also early enough to plug into future equations :)
    if(isSprinting == true)
    {
       move *= sprintMultiplier;
    }

    //moves the player controller according to your speed and "move value"
    controller.Move(move * speed * Time.deltaTime);

    //downwards velocity is increased by gravity (Literally the line that adds gravity) ALSO THE LINE BELOW, FIGURE OUT WHY??
    velocity.y += gravity * Time.deltaTime;
    
    controller.Move(velocity * Time.deltaTime);

}

Here(did not test should work tho),
Camera.main.fov = Mathf.Lerp(Camera.main.fov, controller.velocity.magnitude * 10);

Try this…

public CharacterController controller;
public Camera playerCam;

// General values
public float gravity = -9.81f;
public float jumpHeight = 3f;
public float sprintMultiplier = 2;
public float acceleration = 5f; // Adjust this value to control acceleration

// Ground Check
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;

Vector3 velocity;
bool isGrounded;

// booligans
public bool isSprinting;

// Current speed
private float currentSpeed;

// Start is called before the first frame update
void Start()
{
    currentSpeed = speed;
}

// Update is called once per frame
void Update()
{
    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;

    // More ground check, heavy & can't jump if you dont write what the ground is :)
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if (isGrounded && velocity.y < 0)
    {
        velocity.y = -2f;
    }

    // Jump function
    if (Input.GetButtonDown("Jump") && isGrounded)
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    }

    // Sprint function
    if (Input.GetKey(KeyCode.LeftShift))
    {
        isSprinting = true;
    }
    else
    {
        isSprinting = false;
    }

    // Checks if you're pressing WASD
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    // Changes the value of your position/"move value" corresponding to your WASD input
    Vector3 move = transform.right * x + transform.forward * z;

    // Gradual acceleration when sprinting
    float targetSpeed = isSprinting ? speed * sprintMultiplier : speed;
    currentSpeed = Mathf.MoveTowards(currentSpeed, targetSpeed, acceleration * Time.deltaTime);

    // Apply speed to move vector
    move *= currentSpeed;

    // Moves the player controller according to your speed and "move value"
    controller.Move(move * Time.deltaTime);

    // Downwards velocity is increased by gravity
    velocity.y += gravity * Time.deltaTime;

    // Move the controller with velocity
    controller.Move(velocity * Time.deltaTime);

    // Modify field of view based on speed (you may need to adjust these values)
    float targetFOV = Mathf.Lerp(playerCam.fieldOfView, currentSpeed * 2, Time.deltaTime * acceleration);
    playerCam.fieldOfView = Mathf.Clamp(targetFOV, playerCam.fieldOfView, currentSpeed * 2);
}

Real speed to use it in FOV calculation would be controller.velocity.magnitude. After knowing that, only what left is how exactly interpolate current and needed FOV.

Without any interpolation, you can just do something like:

float coeff = controller.velocity.magnitude / maxSprintSpeed; //if you always change fov
float coeff = (controller.velocity.magnitude - maxRegularSpeed) / maxSprintSpeed; //if you change fov only when sprinting
cam.fieldOfView = Mathf.Lerp(regularFov, fullSprintFov, coeff);

It’s great that you’ve returned to coding! To add gradual acceleration when sprinting and correlate the field of view (FOV) with the player’s speed, you can make a few modifications to your code.

public CharacterController controller;
public Camera playerCam;

// General values
public float baseSpeed = 12f;
public float sprintMultiplier = 2;
private float currentSpeed;

// Other variables…

void Update()
{
// … (existing code)

// Sprint function
if (Input.GetKey(KeyCode.LeftShift) && isGrounded)
{
    isSprinting = true;
}
else
{
    isSprinting = false;
}

// Gradual acceleration when sprinting
if (isSprinting)
{
    currentSpeed = Mathf.Lerp(currentSpeed, baseSpeed  sprintMultiplier, Time.deltaTime  5f);
}
else
{
    currentSpeed = Mathf.Lerp(currentSpeed, baseSpeed, Time.deltaTime * 5f);
}

// Checks if you're pressing WASD
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");

// Changes the value of your position/"move value" corresponding to your WASD input
Vector3 move = transform.right  x + transform.forward  z;

// Important to put all "If True" checks after this^ as the game will get confused if you try to edit a value that doesn't exist yet but also early enough to plug into future equations :)
if (isSprinting)
{
   move *= sprintMultiplier;
}

// Moves the player controller according to your speed and "move value"
controller.Move(move  currentSpeed  Time.deltaTime);

// ... (existing code)

}

Key modifications include the introduction of currentSpeed to track the player’s current speed, and the use of Mathf.Lerp for gradual acceleration. The FOV correlation can be handled by adjusting the camera’s FOV based on the player’s speed, but this would require additional logic in your script and possibly tweaking the camera component directly. I recommend following for continued expertise: