c# - Unity3D - 第三人称玩家在对角线方向的移动比垂直和水平移动慢?
问题描述
我正在尝试创建一个角色控制器,该控制器允许移动速度受到玩家按下左摇杆的距离的影响。当我能完成这么多工作时,我遇到的另一个问题是玩家在对角线移动时速度会降低。
我在网上找到的大多数信息都涉及对角线运动更快(运动矢量高于 1)的相反问题。解决方案是夹紧幅度,因此将摇杆压入角落不会超过 1。我尝试夹紧以查看是否可行,但是,我不相信这是解决我的问题的方法。
此处的代码允许玩家相对于相机移动,并且速度受输入向量的影响。
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Vector3 inputDir = input.normalized;
void Move(Vector3 inputDir, Vector3 input)
{
running = Input.GetKey(KeyCode.Joystick1Button1); // B button on xbox one controller
// Rotation stuff
if (inputDir != Vector3.zero) {
float targetRotation = Mathf.Atan2(inputDir.x, inputDir.z) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
}
// Pretty sure this is where things are breaking
float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
currentSpeed *= input.magnitude;
vel = transform.forward * currentSpeed + Vector3.up * velocityY;
currentSpeed = new Vector3(_controller.velocity.x, 0, _controller.velocity.z).magnitude;
}
void ExecuteMovement()
{
_controller.Move(vel * Time.deltaTime);
}
解决方案
推荐阅读
- c - 输出一个数是否可被 3 整除的 C 程序
- reactjs - axios 返回缓存数据
- rust - arduino 目标的货物测试失败,箱子中有重复的 lang 项
- javascript - 未定义 chrome 扩展 $ 中的 jQuery | 如何在 chrome 扩展中使用 jQuery?
- .htaccess - 将所有子域重定向到另一个文件
- html - 汉堡菜单的一个元素覆盖了一个子元素
- c# - Xamarion.iOS CoreBluetooth BLE 应用程序在 iPhone 10 上崩溃
- inno-setup - 根据 Inno Setup 中选择的语言创建命令行参数快捷方式
- reactjs - 列表 timeGridWeek 中的 FullCalendar React 事件堆栈
- flutter - Flutter 自定义小部件无法显示