I made a script that allows the player to level up their speed but it can’t read the movement script. The movement script I’m using is the sample assets beta First Person Character.
var level = 1;
var currentStamina : float = 100.0;
var maxStamina : int = 100;
var regenStamina : float = 2.0;
var drainStamina : float = 2.0;
var currentJump : float = 1.0;
var maxJump : float = 1.25;
var sprintSpeed : float = 8;
var strafeSpeed : float = 6;
var walkSpeed : float = 4;
private var playerScript : FirstPersonCharacter;
function Start()
{
playerScript = GetComponent(FirstPersonCharacter);
}
function Update ()
{
//if you're moving and pressing shift then speed up take from stamina bar
if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
{
currentStamina -= Time.deltaTime * drainStamina;
playerScript.runSpeed = sprintSpeed;
}
//if not pressing anything, then normal movement speed
else
{
playerScript.walkSpeed = walkSpeed;
playerScript.strafeSpeed = walkSpeed;
}
//Stamina Regeneration
if(playerScript.walkSpeed == walkSpeed && (currentStamina >= 0))
{
currentStamina += Time.deltaTime * regenStamina;
}
//if moving, pressing shift and stamina is 0 then you cannot sprint
if(Input.GetKey(KeyCode.LeftShift) && currentStamina <= 0)
{
playerScript.walkSpeed = walkSpeed;
}
//if stamina regeneration set to 100, never go past
if(currentStamina >= maxStamina)
{
currentStamina = maxStamina;
}
//if goes to 0, then set to 0
if(currentStamina <= 0)
{
currentStamina = 0;
}
}