if(crouch){//如果下蹲标志位为真
if(controller.height < movement.crouchHeight+0.01f && controller.height > movement.crouchHeight-0.01f)//如果这些满足条件
return;//返回
controller.height = Mathf.Lerp(controller.height, movement.crouchHeight, Time.deltaTime/movement.crouchSmooth);//使用线性插值改变角色控制器高度
Vector3 tempCenterY = controller.center;//得到角色控制器中心点
tempCenterY.y = Mathf.Lerp(tempCenterY.y, movement.crouchHeight/2, Time.deltaTime/movement.crouchSmooth);//线性插值改变角色控制器中心点位置
controller.center = tempCenterY;//赋值
Vector3 tempPos = lookObj.transform.localPosition;//得到lookObj对象的位置
tempPos.y = Mathf.Lerp(tempPos.y, movement.crouchHeight, Time.deltaTime/movement.crouchSmooth);//线性插值改变位置
lookObj.transform.localPosition = tempPos;//赋值
movement.maxForwardSpeed = movement.crouchSpeed;//赋值
movement.maxSidewaysSpeed = movement.crouchSpeed;//赋值
movement.maxBackwardsSpeed = movement.crouchSpeed;//赋值
}
//when there is no crouch and character controller height < standard Height-0.01
if(!crouch && controller.height < standardHeight-0.01f){
//Smooth(平滑) Character controller height to the standard height
controller.height = Mathf.Lerp(controller.height, standardHeight, Time.deltaTime/movement.crouchSmooth);
//不能直接用controller.center。y = 0.5f;zhe种做法是error的,必xu使用temporary量来dui controller.center等lei型的属性jin行fu zhi
Vector3 tempCenter = controller.center;
tempCenter.y = Mathf.Lerp(tempCenter.y,centerY,Time.deltaTime/movement.crouchSmooth);//dui角色控制器的中心点Y坐biao xian性插zhi
controller.center = tempCenter;
Vector3 tempPos = lookObj.transform.localPosition;
//camera坐biao xian性插zhi,使camera随着角色高度的change上下move
tempPos.y = Mathf.Lerp(tempPos.y, standardHeight, Time.deltaTime/movement.crouchSmooth);
lookObj.transform.localPosition = tempPos;
}![![图片说明](https://img-ask.csdn.net/upload/201703/30/1490851346_130702.png)图片说明](https://img-ask.csdn.net/upload/201703/30/1490851336_278522.png)