早上无叶花 2017-03-30 05:18 采纳率: 33.3%
浏览 1877

求帮助,Unity3D角色控制器脚本的问题,不多说,直接上图和代码

 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) 
  • 写回答

1条回答 默认 最新

  • 早上无叶花 2017-03-30 16:41
    关注

    下蹲时是这样的站起来之后就变这样了

    评论

报告相同问题?

悬赏问题

  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据