我一直在尝试制作一个非常基本的fps角色控制器脚本,但我无法解决侧向移动时的移动堆叠问题。我确信这确实是一个基本的解决方案,但作为一个初学者,我很难解决它
float forwardSpeed = Input.GetAxis("Vertical") * movementspeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementspeed;
Vector3 VecForwardSpeed = new Vector3(sideSpeed, verticalVelocity, forwardSpeed);
VecForwardSpeed = transform.rotation * VecForwardSpeed;
characterController.Move(VecForwardSpeed * Time.deltaTime);
如果我理解正确,你的意思是,如果同时向前和侧向移动,这些输入/速度";堆叠";或者总结为允许用户移动得比实际允许的更快。
你可以通过对它们进行归一化来解决这个问题,这意味着你要确保它们的组合永远不会超过1
的幅值,比如
// Get a vector of the combined input
var combinedInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
// Check if the magnitude exceeds 1
// sqrMagnitude is more efficient here and for comparing to 1
// behaves the same as magnitude
if(combinedInput.sqrMagnitude > 1)
{
// If so normalize the input vector to force it again
// to have the maximum length/magnitude of 1
combinedInput.Normalize();
}
// Until then apply the movementspeed here
combinedInput *= movementspeed;
// Now use the components of this combined and evtl normalized input vector instead
var vecForwardSpeed = transform.rotation * new Vector3(combinedInput.x, verticalVelocity, combinedInput.y) * Time.deltaTime;
characterController.Move(vecForwardSpeed);
从你的问题来看,还不确定verticalVelocity
是如何发挥作用的。