- Home /
The question is answered, right answer was accepted
Spam Jumping
Hello! I made a Script for 3D Movement and I'm new, that brings a lot of problems, well only one for now. If I spam Jumping in the Game I wont be able to jump anymore does it have something to do with the code or the objects?
{
public float Movespeed = 5f;
private float Movejump = 5f;
private Rigidbody rb;
private bool grounded = true;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float horizontal = (Input.GetAxis("Horizontal") * Time.deltaTime * Movespeed);
float vertical = (Input.GetAxis("Vertical") * Time.deltaTime * Movespeed);
transform.Translate(horizontal, 0f, vertical);
if (Input.GetButton("Jump") && grounded)
{
rb.AddForce(new Vector3(0, Movejump, 0), ForceMode.Impulse);
grounded = false;
}
if (grounded == false)
{
Movejump = 0;
}
if (grounded == true)
{
Movejump = 5;
}
}
private void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == "Boden")
{
grounded = true;
Debug.Log("Im working");
}
}
}
Answer by Koridor08 · Apr 02, 2020 at 08:24 AM
tldr: Just remove 2 conditionals statements that changes MoveJump value.
If you hold Jump button for the time that your character jumps and falls down this will happen:
Update() -> button is down and grounded is true, so AddForce executes and grounded is set to false
grounded is false so MoveJump is set to 0.
you still hold your Jump button (GetButton("Jump") is still true)
your character falls down
OnCollisionEnter() -> grounded is set to true
Update() -> GetButton("Jump") is still true and grounded is true, MoveJump has value of 0, so AddForce executes with 0 force, then grounded is set to false.
you can't jump anymore, because 'grounded' is false and you're standing on the ground.
Fix: Just remove 2 conditionals statements that changes MoveJump value.
Damn this answer is pretty good, ty very much for the explanation :D
Follow this Question
Related Questions
Why when i press space button nothing was happened 0 Answers
fps jump Brackeys 0 Answers
Jump Speed 1 Answer
Jump Further When Key Is Held Down 1 Answer