My 3D character is flying up when I jump
Hello!
When I jump with my 3D character and I press several times space (jump), then my character fly up in the air. What's the problem ? :/ On the object have mesh collider.
My jump script in FixedUpdate:
if (grounded == true && Input.GetAxis("Jump") > 0)
{
grounded = false;
rb.AddForce(new Vector3(0, jumpHeight, 0));
print("JUMP");
actions.Jump();
}
//check if we are grounded
groundCollisions = Physics.OverlapSphere(groundCheck.position, groundCheckRadius, groundLayer);
if (groundCollisions.Length > 0) grounded = true;
else grounded = false;
You need to implement a kind of cooldown for the jumping. See, even when the character is pushed upwards after pressing space there will still be a moment where he is considered grounded so the user can still press space several more times before the overlap sphere registers no ground collisions anymore. This causes more and more force to get added to the rigidbody. You can also experiment with the other forcemodes, such as impulse.
Answer by Creeper_Math · Jan 04, 2017 at 02:42 PM
As @Cherno says, you need to add a cooldown...
Add a float to the variables to the top, as float timeCounter;
and add the following script to the top of the FixedUpdate or Update (whatever you're using)
timeCounter += -1 * Time.deltaTime;
Then replace your rb.AddForce(new Vector3(0, jumpHeight, 0));
with the following script
if (timeCounter < 0) {
timeCounter = 0.5f; // Or whatever delay you want
rb.AddForce(new Vector3(0,jumpHeight,0));
actions.Jump();
print("Jump");
}
Thank you so very much, it's working, but not correctly. Because the character is jumping when not walking or running, but I start walking, then character doesn't jump.
This is my script for walking/running and jumping. This is under the FixedUpdate.
timeCounter += -1 * Time.deltaTime;
if (grounded == true && Input.GetAxis("Jump") > 0)
{
if (timeCounter < 0)
{
timeCounter = 0.5f; // Or whatever delay you want
rb.AddForce(new Vector3(0, jumpHeight, 0));
actions.Jump();
print("Jump");
}
}
//check if we are grounded
groundCollisions = Physics.OverlapSphere(groundCheck.position, groundCheckRadius, groundLayer);
if (groundCollisions.Length > 0) grounded = true;
else grounded = false;
if (Input.GetAxis("Horizontal") != 0)
{
move = Input.GetAxis("Horizontal");
if (isRunning == true)
{
runSpeed = 100;
actions.Run();
}
else
{
runSpeed = 1;
actions.Walk();
}
}
else
{
runSpeed = 0;
actions.Stay();
}
rb.velocity = new Vector3(move * runSpeed, rb.velocity.y, 0);
Try putting a bunch of debug logs around, ex. putting Debug.log("Checked for Grounding");
right before you check for grounding, and Debug.log("Finished Script");
at the end. If the last one, which should be placed at the very very end, doesn't show up in the debug box, then the script is never getting to or executing the move function, which is a problem
Thanks the answer, I put debug logs, but script is executed.
Script look like this:
timeCounter += -1 * Time.deltaTime;
if (grounded == true && Input.GetAxis("Jump") > 0)
{
if (timeCounter < 0)
{
timeCounter = 0.5f; // Or whatever delay you want
rb.AddForce(new Vector3(0, jumpHeight, 0));
actions.Jump();
print("Jump");
Debug.Log("Checked for Grounding");
}
}
//check if we are grounded
groundCollisions = Physics.OverlapSphere(groundCheck.position, groundCheckRadius, groundLayer);
if (groundCollisions.Length > 0)
{
grounded = true;
}
else
{
grounded = false;
}
if (Input.GetAxis("Horizontal") != 0)
{
move = Input.GetAxis("Horizontal");
if (isRunning == true)
{
runSpeed = 100;
actions.Run();
}
else
{
runSpeed = 1;
actions.Walk();
}
}
else
{
runSpeed = 0;
actions.Stay();
}
rb.velocity = new Vector3(move * runSpeed, rb.velocity.y, 0);
Debug.Log("Finished Script");
Your answer
Follow this Question
Related Questions
Rigidbody looses momentum with contact to ground. Why? 3 Answers
Problem With a Multi-Jump C# script, robot 2d character controller 1 Answer
How to jump to a point using CharacterController? 0 Answers
How to use 3D colliders and rigidbody on 2D character controller 0 Answers
Applying Force to the Character 0 Answers