I’m trying to say that when grounded you can jump which should make isJumping true. However, isJumping isn’t becoming true.
Also, for some reason, I can continuously hit the “w” key and it’ll jump even when isGrounded and isJumping is false.
Shouldn’t it only be allowed to jump if both conditions are met for either of the two scenarios:
Press W and isGrounded
Press W and isJumping
I can’t see why it’s acting the way it is. I’m still very new to coding so I really appreciate any help you can offer.
Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testDJ : MonoBehaviour
{
public GameObject ground;
public Rigidbody rb;
public int jumpforce;
public bool isGrounded;
public bool isJumping;
void FixedUpdate()
{
if (Input.GetKeyDown("w") && isGrounded == true)
rb.AddForce(0, jumpforce, 0);
isJumping = true;
if (Input.GetKeyDown("w") && isJumping == true)
rb.AddForce(0, jumpforce, 0);
isJumping = false;
}
void OnCollisionStay(Collision other)
{
if (other.gameObject.tag == "ground")
isGrounded = true;
}
void OnCollisionExit(Collision other)
{
if (other.gameObject.tag == "ground")
isGrounded = false;
}
}
Looks like you’re missing some curly braces in there to define the scope of your if statements. Also, the logic inside the conditionals are adding the same jumpForce twice in the same frame, the code doesn’t make a lot of sense. Another thing is that the jumpForce is an int, but I’m quite sure there is no overload for AddForce() to allow for the parameters as int’s. Maybe you should try something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testDJ : MonoBehaviour
{
public GameObject ground;
public Rigidbody rb;
public float jumpforce;
bool isGrounded = false;
bool isJumping = false;
void FixedUpdate()
{
if (Input.GetKeyDown("w") && isGrounded == true && !isJumping){
rb.AddForce(0, jumpforce, 0);
isJumping = true;
}
else if (Input.GetKeyDown("w") && isJumping){
rb.AddForce(0, jumpforce, 0);
isJumping = false;
}
}
void OnCollisionStay(Collision other)
{
if (other.gameObject.tag == "ground")
isGrounded = true;
isJumping = false;
}
void OnCollisionExit(Collision other)
{
if (other.gameObject.tag == "ground")
isGrounded = false;
}
Oh I see where the curly braces are missing thank you.
Unfortunately, after trying your code, it still didn’t work.
I think this piece is where the issue is. I made isJumping public so I can see in the inspector if its being toggled to true after I jump and it never is. Do you know why “isJumping = true;” isn’t running after jumping? Sorry about the code below, this site isn’t identifying where the code starts correctly.
void FixedUpdate()
{
if (Input.GetKeyDown(“w”) && isGrounded == true)
{
rb.AddForce(0, jumpforce, 0);
isJumping = true;
}
}
As far as I have been dealing with Physics and Inputs, I know the best approach is to register for inputs in the Update(), while anything related to physics should be in the FixedUpdate(). Simply because Update() runs once every frame, whereas FixedUpdate() runs once every predefined interval of time (0.02s by default).
What I mean you should do is:
-
Place all your input code in the Update()
public void Update ()
{
if (Input.GetKey(Keycode.W) && isGrounded)
{
isGrounded = false;
isJumping = true;
} else if (Input.GetKey(Keycode.W) && isJumping)
{
isJumping = false;
}
}
And then put the physics in the FixedUpdate() like so:
public void FixedUpdate()
{
//FixedUpdate is case sensitive, meaning if you type it like Fixedupdate, it will not work. Be careful there!
if (isGrounded && !isJumping)
{
//Your AddForce code for first jump
} else if (!isGrounded && isJumping)
{
//Your second AddForce code for second jump in mid-air
}
}
You might need to swap GetKey for GetKeyDown (if GetKey is not working), because I suspect that once you hit W, it will initiate the second jump straightaway.