#When I jump I can infinity jump.
1 messages · Page 1 of 1 (latest)
Code and your current player inspector would help
Of course 🙂
private void FixedUpdate()
{
if (jumpKeyWasPressed)
{
GetComponent<Rigidbody>().AddForce(Vector3.up * 5, ForceMode.VelocityChange);
jumpKeyWasPressed = false;
}
Movement();
}
Right there you're accounting for going up. But how do you come back down?
Since you're using a Rigidbody, do you have a gravitational setting applied?
So you just keep repeatedly jumping?
On button press only, right? It doesn't loop jump without doing anything?
and you want a "cooldown"?
So the simplest way, although not necessarily the best is to keep track of when you can next jump
Oh you're still jumping when in the air?
Right so the is Grounded check is in place, let me take a further look
Are you getting valid debug logs with IsGrounded?
well as for me i want to ask you question what's the output of this code when you try to Jump?
void OnCollisionStay(Collision collisionInfo)
{
if (collisionInfo.transform.tag == "Ground")
{
IsGrounded = true;
Debug.Log("Grounded");
}
else
{
IsGrounded = false;
Debug.Log("Not Grounded!");
}
}
Since that's the focus point. If IsGrounded is true when you're in the air, that would be the cause
Just like @frosty swan mentioned. What's that Callback doing
if player was on the ground is it "Grounded"? and when player on the air is it still Grounded or Not Grounded?
Is it ever "Not Grounded"? It should be like so when you're in the air
I find the isGrounded check a bit odd honestly. This will trigger for any collision that remains touching.
https://docs.unity3d.com/ScriptReference/Rigidbody.OnCollisionStay.html
I think making sure you have "surface" below you before you can jump again would be the more appropriate approach of checking isGrounded
Is the ground jumping with you?
i think the problem is your collider somehow still detect the ground... even though player is in the air.
Well the OnCollisionStay isn't going to trigger when you leave the ground, unless you're colliding with something else.
Try jumping into a wall, that's not marked as a Ground tag. Then you probably can't jump again.
@severe night are you jumping infinitely or just once after leaving the ground?
This!
try OnCollisionEnter instead
Make sure it's not marked as "Ground". If you collide with it as well, you're going to have it trigger your onCollisionStay and override isGrounded to false. But in truth this is a confirmation that your grounded logic is flawed and should be reconsidered.
It prints the name of whatever you are colliding with
A simpler solution would be to, on FixedUpdate, just before you decide whether or not to jump, is to make an isGrounded check yourself using a sphere collision check
I don't think onCollisionStay is going to do what you want.
That's for when you're staying in a collider. Like a "SUPERMAN AURA COLLIDER". As long as you stay in it you can make your character's head grow 3 inches per second 😛
in my knowladge if you have OnCollisionStay you should have OnCollisionExit (CMIIW)
Debug.Log(collisionInfo.gameObject.name);
Will give more info
The object is called Grounded?
Well then my code isn’t running
Well whatever it is, it is colliding with it
@severe night anyways, use OnCollisionEnter that sets you to grounded, and OnCollisionExit to set Grounded = false
AnyWhere where you want, but it's the best practice to put it right under OnCollisionStay ( Easy to read when others try to understand your code )
yep, and add this
if(collision.transform.tag == "Ground")
{
IsGrounded = false;
}
tbh i rarely people uses this method and i haven't search it yet best use for OnCollisionStay/Exit
yep
You’ve never used OnCollisionEnter?
Yes that works
Also why did you put my debug in the ungrounded part of we knew it wasn’t running
It’s fine but thought I’d point that out
or you could do something like this
void OnCollisionEnter(Collision other)
{
if(other.transform.tag == "Ground")
{
IsGrounded = true;
}else
{
IsGrounded = false;
}
}
well you don't know if you didn't try it out
i wound't tell you tho xD try it
Anyways, in the future use CompareTag instead of ==. It’s tip in #💻┃code-beginner’s tip
Agree with this
Just keep in mind if you're Entering another collision after having entered a ground collision, it can set your IsGrounded to false when it's still true.
tag is old one, and CompareTag is new one and efficient if im not wrong
As I said, checking regularly for a surface below you through a Physics call it might be preferred.
You can make a simple GameObject within your player, move it down to the legs and do a Physics.SphereCheck upon that gameObject
This is to avoid facing issues further on when you end up with multiple collisions at once
As long as you have a ground below you, you should be able to jump
You welcome have a nice day!
Ehh it's fine, just place it at the bottom part of your character that touches the ground
Just create child objects
depends on what games you are trying to makes
its your rule not the player i think
Physics.CheckSphere(footCollider.position, 0.5f);
As an example