问题描述
我通过 youtube 教程创建了一个刚体 fps 控制器,除了对角线移动之外,一切正常,因为它的 x1,4 速度有问题。所以我尝试了 vector3.normalized 和 vector3.ClampMagnitude 但似乎这不是我的 fps 控制器解决方案的一个选项,因为速度变得恒定且不可改变。 原始代码:
x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
z = Input.GetAxis("Vertical") * speed * Time.deltaTime;
Vector3 moveVec = orientation.right * x + orientation.forward * z + Vector3.up * rb.veLocity.y;
rb.veLocity = moveVec;
所以我试过了
x = Input.GetAxis("Horizontal") ;
z = Input.GetAxis("Vertical");
Vector3 moveVec = Vector3.ClampMagnitude(orientation.right * x + orientation.forward * z +
Vector3.up * rb.veLocity.y,1.0f) * speed * Time.deltaTime;
rb.veLocity = moveVec;
它起作用了,但是当我撞到一个物体并且一些随机的 Y 变换会发生时,我在空中飞行,因为 rb.veLocity 也乘以速度和 deltaTime。有什么提示吗?
解决方法
将您的输入与当前速度分开,并将 speed
乘数仅应用于输入。
请注意,您正在处理以每秒 Unity 单位为单位的 速度。 => 你不想想在这里乘以Time.deltaTime
!
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
Vector3 moveVec = Vector3.ClampMagnitude(orientation.right * x + orientation.forward * z,1.0f) * speed;
moveVec += Vector3.up * rb.velocity.y;
rb.velocity = moveVec;