hi everyone,
I’ve made this movement script that’s attached to my player. It controls the player walking, running with a resource that decreases when the run button (left shift) is pressed, regenerates the resource when not running and has a regeneration ‘delay’ when the resource is fully depleted. The code is working as I’d like, I’m just worried that i may be taking the long way around on this and that there is a more efficient way to do this (less lines of code is better right?).
Here is my code
#pragma strict
var playerSpeed : float = 10.0;
var playerWalkSpeed : float = 10.0;
var playerRunSpeed : float = 50.0;
var playerRunPower : float = 50.0;
var playerRunPowerMax : float = 50.0;
var playerRunPowerDecrease : float = 1.0;
var playerRunPowerIncrease : float = 1.0;
var playerRunWait : float = 5.0;
var playerRunWaitIncrease : float = 1.0;
var playerRunBuffer : float = 5.0;
private var player :CharacterController;
player = gameObject.GetComponent(CharacterController);
function Update(){
if(Input.GetKey(KeyCode.LeftShift) && playerRunPower > 0 && playerRunWait >= 5 && player.velocity.magnitude > 0){
if(Input.GetAxis ("Vertical") || Input.GetAxis ("Horizontal")){
playerSpeed = playerRunSpeed;
playerRunPower -= playerRunPowerDecrease * Time.deltaTime;
}
}
else{
playerSpeed = playerWalkSpeed;
if(playerRunPower < 1){
playerRunWait = 0;
}
if(playerRunWait < playerRunBuffer){
playerRunWait += playerRunWaitIncrease * Time.deltaTime;
}
if(playerRunPower < playerRunPowerMax){
playerRunPower += playerRunPowerIncrease * Time.deltaTime;
}
}
}
function FixedUpdate(){
var movement = Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")) * Time.deltaTime * playerSpeed;
player.Move(movement);
}
@script RequireComponent(CharacterController)