I have been trying to properly limit movement on my 3D fps controller, and I have so far just been using basic clamping to limit the speed on the X and Z axis. The problem with this is that any external forces will never cause the player to exceed its predefined maximum speed, meaning that external forces like explosions do basically nothing to move the player like an explosion should. My idea for getting this to work was to only allow additional movement input if the player was below a certain maximum velocity, but so far I have been unable to get it to work in code. I am fairly new to working in 3D space with vectors, so I am pretty lost on what to do. My attempted implementation worked until the player turned their head while moving, at which point the movement would not be limited. Here is the code I have tried:
xInput = Input.GetAxisRaw("Horizontal");
yInput = Input.GetAxisRaw("Vertical");
if (xInput > 0 && transform.InverseTransformDirection(rb.velocity).x > inputMaximumSpeed) xInput = 0;
if (xInput < 0 && transform.InverseTransformDirection(rb.velocity).x > -inputMaximumSpeed) xInput = 0;
if (yInput > 0 && transform.InverseTransformDirection(rb.velocity).z > inputMaximumSpeed) yInput = 0;
if (yInput < 0 && transform.InverseTransformDirection(rb.velocity).z > -inputMaximumSpeed) yInput = 0;
//Apply Movement
moveDirection = (xInput * transform.right + yInput * transform.forward).normalized;
rb.AddForce(moveDirection * groundAccSpeed);
Not too sure what to do to fix this, as I have found little to none in terms of examples on how others have gotten this to work. Thanks for any help!