- Home /
Character jumps to high on start moving
So I have got a player which needs to move and be able to jump. The player moves just fine but when I jump while pressing any movement keys it jumps about 4-5 times higher than usual.
Code: void FixedUpdate() { Movement(); }
private void Movement()
{
if (airControl || grounded)
{
float h = Input.GetAxis("Horizontal") * moveSpeed / 2000;
float v = Input.GetAxis("Vertical") * moveSpeed / 2000;
rb.MovePosition(transform.position + (transform.forward * v) + (transform.right * h));
if (Input.GetButton("Run"))
{
moveSpeed = runSpeed;
}
else
{
moveSpeed = walkSpeed;
}
if (Input.GetButtonDown("Jump") && grounded)
{
Jump();
}
}
grounded = false;
}
Thanks in advance.
Answer by logicandchaos · Feb 01, 2020 at 07:10 PM
I think it's this line: float h = Input.GetAxis("Horizontal") * moveSpeed / 2000; You are setting the horizontal movement to your moveSpeed, so the faster you are moving the higher you will jump. You should also post your Jump function to see if it's an issue in there.
Answer by stefanheuvink · Feb 26, 2020 at 08:19 AM
I changed my code a bit and now it looks like this:
float h = 0;
float v = 0;
if (Input.GetKey(kc.keys.forward))
v = 1;
else if (Input.GetKey(kc.keys.back))
v = -1;
if (Input.GetKey(kc.keys.right))
h = 1;
else if (Input.GetKey(kc.keys.left))
h = -1;
Vector3 movement = new Vector3(h, 0, v).normalized;
h = movement.x;
v = movement.z;
Vector3 desiredForward = Vector3.Normalize(new Vector3(cam.transform.forward.x, 0, cam.transform.forward.z));
Vector3 desiredRight = Vector3.Normalize(new Vector3(cam.transform.right.x, 0, cam.transform.right.z));
rb.AddForce(desiredForward * v * speed);
rb.AddForce(desiredRight * h * speed);
if (Input.GetKeyDown(kc.keys.jump) && grounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
Your answer
Follow this Question
Related Questions
isGrounded is always false, even with gravity, how do you fix that? 1 Answer
Player Character looses jump height the further right they go 1 Answer
Why won't my character jump,How do I make my character jump? 2 Answers
how to move and jump at the same time,how to move and jump at the same time 0 Answers