I am back once again. This time with a basic 2D platformer game. I would like to allow my player to jump but only if the ground bool checks as true. I would like to fix my script so that upon collision with the ground the player is able to jump, but while the player is in the air the player is not able to jump. My platform has a box collider and my player has a collider as well as a rigidbody. How do I go about fixing my script to allow this system to go into effect? Please and Thank you!
public class MovePlayer : MonoBehaviour {
public float speed = 0f;
public float height = 0f;
public GameObject player;
private bool grounded = false;
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "ground")
grounded = true;
return;
}
// Use this for initialization
void Movement ()
{
if (Input.GetKey (KeyCode.RightArrow))
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.Translate (Vector3.left * speed * Time.deltaTime);
}
while (grounded = true)
{
Jump();
}
}
void Jump()
{
if (Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKey (KeyCode.Space)) {
transform.Translate (Vector3.up * height * Time.deltaTime);
return;
}
}
// Update is called once per frame
void Update ()
{
Movement ();
}
}