If player's speed is over 5 change field of view

Hi everyone i need help. I want when my character’s speed is over a number, the view of field of the camera to change. I’m using this script but it works wrong everytime the speed is over 5 it just keeps going from 70 to 80.

var runSpeed: float = 5;
private var lastPos: Vector3;

function Update()
{
var dist = Vector3.Distance(transform.position, lastPos);
lastPos = transform.position;

if (Time.deltaTime > 0)
{
var speed = dist / Time.deltaTime;

if (speed > runSpeed)
{
Camera.main.fieldOfView = 80;
}
if (speed < runSpeed)
{
Camera.main.fieldOfView = 70;
}
}
}

What are u using to adjust the runSpeed ? Tracking velocity is a good way to find out your current speed.

I’m using this to adjust the speed but i need the script to change the view of the field of the camera when im runing and go back to normal when i no longer run.

#pragma strict

var walkSpeed : float = 7; // Regular speed
var sprintSpeed : float = 13; // Run speed

private var charMotor : CharacterMotor;
private var charController : CharacterController;

function Start () 
{
	charMotor = GetComponent(CharacterMotor);
	charController = GetComponent(CharacterController);
}

function Update () 
{
	//Making the actual speed var
	var speed = walkSpeed;
	
	//Checking for oppertunity to sprint
	if ( charController.isGrounded  Input.GetKey("left shift") || Input.GetKey("right shift"))
	{
		//changing the speed to sprint
		speed = sprintSpeed;
	}
	
	charMotor.movement.maxForwardSpeed = speed; //Setting the speed
}

something like this should work…

var _mainCamera : Camera;

function Start () 
{
    charMotor = GetComponent(CharacterMotor);
    charController = GetComponent(CharacterController);
    _mainCamera = Camera.main;
}


function Update () 
{

    //Making the actual speed var
    var speed = walkSpeed;
    var fov = 70;
  
    //Checking for oppertunity to sprint
    if ( charController.isGrounded  Input.GetKey("left shift") || Input.GetKey("right shift"))
    {
        //changing the speed to sprint
        speed = sprintSpeed;
        fov = 80;
    }
  
    charMotor.movement.maxForwardSpeed = speed; //Setting the speed
    _mainCamera.fieldOfView = fov;

}

I get many unknown identifiers…walkSpeed, charController, sprintSpeed,charMotor.

You can simply deduce the meaning of them by looking at the name or how they are used, its not that hard :wink:

Looks like JamesLeeNZ only filled in the lines u need to add to your above example. - lol

Can you write me the full script please ?