I created a rigidbody fps controller by youtube tutorial and everything works fine except diagonal movement as it x1,4 speed bugged. So i tried vector3.normalized and vector3.ClampMagnitude but seems this is not an option for my fps controller solution as speed becomes constant and unchangable. Original code:
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;
So i tried this
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;
And it worked but when i hit an object and some random Y transform accures i fly in the air as rb.velocity is also multiplyed on speed and deltaTime. Any tips?
Separate your Input from the current velocity and apply the speed multiplier only to the input.
Note that you are dealing with a velocity which is in Unity Unit per second. => You do not want to multiply by Time.deltaTime here!
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;