Unity3d 防止相机“穿墙”加移动跳跃


该感谢分享:具体实现看第一第三人称控制器

 private void CameraOcclusion()
    {

        Vector3 CameraPointing = transform.position - MainCameraModel.transform.position;//相机到人物的向量
        Ray CameraRay = new Ray(MainCameraModel.transform.position, CameraPointing);
        RaycastHit hitInfo;
        if (Physics.Raycast(CameraRay, out hitInfo))
        {

            if (hitInfo.collider.tag != "Player")
            {
                _mesList.Add(hitInfo.collider.gameObject.GetComponent<MeshRenderer>());
                hitInfo.collider.gameObject.GetComponent<MeshRenderer>().enabled = false;

            }
            else
            {
                foreach (var item in _mesList)
                {
                    item.enabled = true;
                }
                _mesList.Clear();
            }

        }

    }

    /// <summary>
    /// 运动
    /// </summary>
    private void Move()
    {

        WalkingSpeed = Input.GetKey(KeyCode.LeftShift) ? RunningSpeed : 2.0f;//加速
        if (GroundCheck)//跳跃
        { 
            _Dontjump = Input.GetKeyDown(KeyCode.Space) ? JunpHeiqht : _Dontjump;
            GroundCheck = false;
        }
        if (_characterController.isGrounded)
        {
            GroundCheck = true;
        }
        float _hprizontal = Input.GetAxis("Horizontal");
        float _vertical = Input.GetAxis("Vertical");

        _moveDirection = new Vector3(_hprizontal * WalkingSpeed, _Dontjump, _vertical * WalkingSpeed);
        _moveDirection = transform.TransformDirection(_moveDirection);

        _Dontjump += Gravity * Time.deltaTime;//重力
        _characterController.Move(_moveDirection * Time.deltaTime);

        Vector3 _movement = Vector3.zero;
        _movement = transform.forward * _vertical + transform.right * _hprizontal;
        _isKeyboard = _movement == Vector3.zero ? _isKeyboard : true;//判断是否有按键输入
        if (_isKeyboard && !FirstPersonExchange)//人物自身旋转
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, CameraFather.transform.rotation, 0.15f);
            Quaternion PlayerModelsRotation = Quaternion.LookRotation(_movement);
            PlayerModelsTransform.rotation = Quaternion.Lerp(PlayerModelsTransform.rotation, PlayerModelsRotation, Time.deltaTime * PlayerRotationSpeed);
            _isKeyboard = false;
        }

}

Unity 角色控制器的几种实现方式(CharacterController驱动,刚体驱动,动画驱动) - 小辉歌 - 博客园 (cnblogs.com)d​​​​​​​jhttps://www.cnblogs.com/xiaoahui/p/13765561.html

(18条消息) 第一第三人称控制器可切换_manzhushahuaxiao的博客-CSDN博客

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值