- Home /
Strange Behaviour during jump and double jump in 3D
The problem is the following: when i press "jump" btn sometimes it jumps very low and sometimes it goes very high. If i try to jump during diagonal movement on the ground, sometimes it doesn't even jump. Why is that, and how can i fix that?
private void FixedUpdate()
{
PlayerMovement();
}
void PlayerMovement()
{
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
Vector3 playerMovement = new Vector3(hor, 0f, ver) * speed * Time.deltaTime;
if (isGrounded)
{
transform.Translate(playerMovement, Space.Self);
}
else
{
transform.Translate(playerMovement * 0.5f, Space.Self); // movement on x-z plane is slowed down during jump
}
if (isGrounded)
{
canDoubleJump = true;
if (Input.GetButtonDown("Jump"))
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
else
{
if (Input.GetButtonDown("Jump") && canDoubleJump)
{
rb.AddForce(Vector3.up * jumpForce * doubleJumpMultiplier, ForceMode.Impulse);
canDoubleJump = false;
}
}
}
private void OnCollisionEnter(Collision other)
{
if (other.transform.tag == "Cube" || other.transform.tag == "Ground")
{
isGrounded = true;
}
}
private void OnCollisionExit(Collision other)
{
if (other.transform.tag == "Cube" || other.transform.tag == "Ground")
{
isGrounded = false;
}
}
Comment