- Home /
If Grounded
Hello everybody! I want to make a jumping function, and I can jump, but I am writing an if grounded variable. My code so far is:
using UnityEngine;
using System.Collections;
public class jumpAfterTest : MonoBehaviour
{
public bool isGrounded = true; //specifies if touching gameobject floor_big
// in this case, it specifies is touching ground
//therefore, mole can jump
public float jumpheight = 3500;
void update()
{
isGrounded = false;
}
public void JAT ()
{
Debug.Log("Me is JAT");
//add effect here (old note)
if (isGrounded == true){
GetComponent<Rigidbody>().AddForce (transform.up * jumpheight);
}
//need if grounded variable (old note)(not working)
}
void OnCollisionEnter (Collision col1)
{
if (col1.gameObject.name == "floor_big")
{
isGrounded = true;
//Destroy(col1.gameObject);
}
}
}
I can jump, but I can also double jump. And triple jump, quadruple jump, quintuple jump, etc. Basically I can fly. I want to limit to one jump, like in real life. I don't know why my script isn't working. Please help out. Thanks! By the way, I am relatively new.
Answer by HarshadK · Aug 18, 2015 at 01:29 PM
Remove
isGrounded = false;
from your Update()
method since it is setting you to be not grounded each frame which is allowing you to have multiple jumps.
And place that isGrounded = false;
after your line:
GetComponent<Rigidbody>().AddForce (transform.up * jumpheight);
inside your JAT ()
method.
Answer by Nirvana33 · Aug 18, 2015 at 01:29 PM
You should add "isGrounded=false" after using addForce.
if (isGrounded == true){
GetComponent<Rigidbody>().AddForce (transform.up * jumpheight);
isGrounded=false;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
2D C# Jump Script Addforce 2 Answers