I can't do jump in my 2D game
Script is: public class PlayerMovement : MonoBehaviour { public CharacterController2D controller; float horizontalMove = 0f; public float runSpeed = 40f; public float jumpForce = 1000f; public Rigidbody2D rb; public Vector2 jumpHeight; // Start is called before the first frame update void Start() {
}
// Update is called once per frame
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
}
void FixedUpdate ()
{
controller.Move(horizontalMove * Time.fixedDeltaTime, false, false);
if(Input.GetKey(KeyCode.Space)) {
rb.AddForce(jumpHeight, ForceMode2D.Impulse);
}
}
}
It have a movement script, it working by CharacterController script, which I took from Internet. I don't have problem with it. But jumping... I don't have any errors, but when I pressing Space, my character is start flying.
P.S. English - not my native language, so sorry for mistakes in sentences.
Answer by sztobar · Oct 03, 2021 at 12:05 PM
That's because your adding a jumpHeight force constantly even when character is in the air. Your character should instead jump only when it's standing on the ground. If you google for ground check
there's plenty of tutorial how to achieve that.
You can look at this video explaining various solutions to this: https://www.youtube.com/watch?v=c3iEl5AwUF8
Your answer
Follow this Question
Related Questions
I have got bugs when i jump 0 Answers
Jumping on enemies help 0 Answers
How do I disable Jumping while on my JumpPad? 1 Answer
Having trouble with 2D Jumping 0 Answers