I have a very specific problem with my rigid body controller. I made my controls add force to the players rigid body, which works ok; so i than added a way for a player to move diagonally, but if the player moves diagonally and one movement key is released the player briefly stops with very visible hitch since the camera is following the player.
I have setup my controller to use FixedUpdate() and also my camera usses FixedUpdate(). Is this correct approach?
How do i make my player not make a brief stop when one of the movement keys is released eg. if i hold down up and right movement key and than release just one of those two, the player briefly stops? I don’t want this to happen. Here is my player movement part of code
if (isGrounded == true)
{
// move player forward
if (Input.GetKey(KeyCode.W))
{
rb.AddForce(transform.forward * speed * Time.deltaTime);
rb.velocity = Mathf.Clamp (rb.velocity.magnitude, 0, speed) * rb.velocity.normalized;
}
// move player backward
if (Input.GetKey(KeyCode.S))
{
rb.AddForce(-(transform.forward) * speed * Time.deltaTime);
rb.velocity = Mathf.Clamp (rb.velocity.magnitude, 0, speed) * rb.velocity.normalized;
}
// move player left
if (Input.GetKey(KeyCode.A))
{
rb.AddForce(-(Camera.main.transform.right) * speed * Time.deltaTime);
rb.velocity = Mathf.Clamp (rb.velocity.magnitude, 0, speed) * rb.velocity.normalized;
}
// move player right
if (Input.GetKey(KeyCode.D))
{
rb.AddForce(Camera.main.transform.right * speed * Time.deltaTime);
rb.velocity = Mathf.Clamp (rb.velocity.magnitude, 0, speed) * rb.velocity.normalized;
}
// move if direction is combined
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) ||
Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
{
rb.AddForce(transform.forward * (speed / 8 * Time.deltaTime)); // ??? not sure about this math here - seems to give desired result
rb.velocity = Mathf.Clamp (rb.velocity.magnitude, 0, speed) * rb.velocity.normalized;
}
// stop moving if movement keys are released
// this also causes the player to briefly stop in place if combination of direction keys is held down and than one gets released
if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D))
{
if (rb.velocity.magnitude > 0)
{
//rb.velocity = Vector3.zero;
//rb.angularVelocity = Vector3.zero;
rb.AddForce(-rb.velocity, ForceMode.VelocityChange);
}
}
// stop moving if opposing direction keys are pressed
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D))
{
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
// jump
if (Input.GetKeyDown("space"))
rb.AddForce(0, 500.0f, 0);
}
}