Basically I’ve attached a script to my 2d sprite so whenever I press the spacebar it jumps, but cannot jump in mid-air and must be touching the ground (another object with a 2d collider) but the sprite doesn’t jump when I press the spacebar (although it does land on the ground). Here’s the script:
public class jellyscipt : MonoBehaviour {
public Vector2 jumpSpee = new Vector2(0, 20);
public float jumpSpeed = 20f;
public bool isGrounded=false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.Space) && isGrounded == true)
transform.Translate (0f, jumpSpeed * Time.deltaTime, 0f);
}
void OnTriggerEnter2D(Collider2D other){
if (other.gameObject.name == "tilegrass") {
isGrounded = true;
}
}
void OnTriggerExit2D(Collider2D other){
isGrounded = false;
}
}
I’m not sure what’s wrong. Any help would be really appreciated, and even a more efficient alternative code would be welcome.