var CrouchHeight = 0.5;
var CrouchVelocity = 0.05;
//var isCrouched : boolean = false;
private var isCrouching : boolean = false;
var walkSpeed : float = 2.0;
var bobbingAmount : Vector2 = Vector2(0.1, 0.05);

private var controller : CharacterController;
private var walker : CharacterMotor;
private var HeadUp : HeadbobberGun;
private var OriginalHeight;
private var OriginalPosition;
private var walkerwalkSpeed = 0.0;
/*private var walkerrunSpeed = 0.0;
private var walkerjumpSpeed = 0.0;*/
private var OrigHeadUpAmount : Vector2 = Vector2.zero;
private var OrigHeadUpSpeed = 0.0;


function Start () {
	controller = GetComponent(CharacterController);
	walker = GetComponent(CharacterMotor);
	HeadUp = this.gameObject.GetComponentInChildren(HeadbobberGun);
	OriginalHeight = controller.height;
	OriginalPosition = transform.position;
	walkerwalkSpeed = walker.movement.maxForwardSpeed;
	OrigHeadUpSpeed = HeadUp.bobbingSpeed;
	OrigHeadUpAmount = HeadUp.bobbingAmount;
}



function Update () {
	if (Input.GetButton ("Crouch")){
		if (!FPCStatus.isCrouched){
			walker.movement.maxForwardSpeed = walkSpeed;
			walker.movement.maxSidewaysSpeed = walkSpeed;
			walker.movement.maxBackwardsSpeed = walkSpeed;
			HeadUp.bobbingAmount =  bobbingAmount;
			HeadUp.bobbingSpeed =  OrigHeadUpSpeed / 2;
			if(controller.height > CrouchHeight){
				controller.height -= CrouchVelocity * Time.deltaTime;
				isCrouching = true;
			}
			else
			if(controller.height < CrouchHeight){
				controller.height = CrouchHeight;
				FPCStatus.isCrouched = true;
				isCrouching = false;
			}
        }
	}
	else{
		if (FPCStatus.isCrouched || isCrouching){
			if(controller.height < OriginalHeight){
				controller.height += CrouchVelocity * Time.deltaTime;
				if(transform.position.y <= OriginalPosition.y)
					transform.position.y += CrouchVelocity * Time.deltaTime;
			}
			else{
				controller.height = OriginalHeight;
				walker.movement.maxForwardSpeed = walkerwalkSpeed;
				walker.movement.maxSidewaysSpeed = walkerwalkSpeed;
				walker.movement.maxBackwardsSpeed = walkerwalkSpeed;
				HeadUp.bobbingAmount =  OrigHeadUpAmount;
				HeadUp.bobbingSpeed =  OrigHeadUpSpeed;
				FPCStatus.isCrouched = false;
				isCrouching = false;
			}
        }
	} 	
}	
	
   