- Home /
Infinite jump still happening,Infinite jump still happening
this should work right?
public Rigidbody rb;
private bool isGrounded = false;
void FixedUpdate () {
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(0, 10000 * Time.deltaTime, 0);
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.name == "ground")
{
if (isGrounded == false)
{
isGrounded = true;
}
}
}
}
,why isnt this working?
public Rigidbody rb;
private bool isGrounded = false;
void FixedUpdate () {
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(0, 10000 * Time.deltaTime, 0);
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.name == "ground")
{
if (isGrounded == false)
{
isGrounded = true;
}
}
}
}
So this finally worked (the Debug.Logs are just to see if it worked correctly)
public class Jump : $$anonymous$$onoBehaviour
{
public float jumpForce = 10000f;
public Rigidbody rb;
private bool isGrounded = false;
void FixedUpdate()
{
if (isGrounded == true && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
rb.AddForce(0, jumpForce * Time.deltaTime, 0);
jumpForce = 0f;
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.name == "ground")
{
Debug.Log("Yes");
{
isGrounded = true;
if (isGrounded == true)
{
jumpForce = 10000f;
Debug.Log("Yes");
}
}
}
}
}
Answer by hameed-ullah-jan · Nov 07, 2018 at 02:47 PM
Try this thing: if (isGrounded == true && Input.GetKeyDown(KeyCode.Space)) { rb.AddForce(0, 10000 * Time.deltaTime, 0); isGrounded == false; }
Doesnt seem to work, "Only assignment, decrement, await, and new object expressions can be used as a statement"
try using isGrounded = false ins$$anonymous$$d isGround == false in the last line
@swanijam dont know what youre talking about,they are all isGrounded
the symbol "=" vs. "==", not the variable names. I didn't mean to change the capitalization on IsGrounded.
isGrounded = false means "set isGrounded to false" and isGrounded == false means "is isGrounded already set to false?"
the code above does the latter, which causes an error because you can make a conditional statement/question like that outside of an if statement.
Answer by RustyCrow · Nov 07, 2018 at 07:05 PM
I am understanding your question in 2 ways.
1. "I press space and my character dosent come down" --> check Rigidbody settings. The scripts should work, this feels like you dont have the propper settings/values in the Rigidbody componant. Check mass, drag, gravity.
2. I can press Space even when in the air -> You need to set isGrounded = false; when you press space
if this still dosent help you @Reissu_Mies we will need some more info like what is your expectations and the data surrounding your jump logic.
Your answer
Follow this Question
Related Questions
Touch buttons for step movememt 3 Answers
Accelerometer 2 Answers
MouseLook character is acting wierd 1 Answer
2D Vector Movement,Vectorel Movement with Buttons 2 Answers
HOW TO MAKE A NPC GO FORWAR 0 Answers