# My character jumps, stops and if I try to jump it jumps!

There is this thing. I have Coded to check if my character is grounded. And it works fine. But problem is that I have added a code to jump. When my character jumps while checking my character is grounded it works but when the character finishes jumping and tries to return to ground boolean isgrounded changes to true even if it isn't grounded. Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class pScript : MonoBehaviour {
public Rigidbody2D rb;
[SerializeField]
public Transform[] Points;
[SerializeField]
[SerializeField]
public bool jump;
public bool isgrounded = false;
[SerializeField]
public float MoveSpeed;
[SerializeField ]
public float jumpHigh;
public bool facingRight;
public float localScale;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();

}

// Update is called once per frame
void Update () {
isgrounded = isGrounded ();
float horizontal = Input.GetAxis ("Horizontal");
rb.velocity = new Vector2 (horizontal * MoveSpeed, rb.velocity.y);
if (Input.GetKeyDown (KeyCode.Space)) {
jump = true;

}
if (isgrounded && jump == true) {
isgrounded = false;
rb.velocity = new Vector2 (0, jumpHigh);

}

if (isgrounded == false) {
jump = false;
}

}
private bool isGrounded(){

if (rb.velocity.y <= 0) {
foreach (Transform points in Points) {
Collider2D[] collider = Physics2D.OverlapCircleAll (points.position, radius, layer);
for (int i = 0; i < collider.Length; i++) {
if (collider [i].gameObject != gameObject) {
return true;
}

}

}

}return false;

}
}

• That's an unusual grounded check function. Does your character have a particularly unconventional shape or construction that makes this complex approach necessary over simpler options? If so, can you walk us through how this character is set up? – DMGregory Nov 20 '18 at 14:31
• To remind you, I am a noob and had been in this game development for only around 3-4 months. I have been watching tutorial in youtube channel named "Inscope Studios." To make you sure, I haven't quite got some of the code of the isgrounded function. The real thing is, I have a character and on his foot , there are three empty objects. This function checks if those three empty object is colliding0-= – Sabin Pyakurel Nov 20 '18 at 14:38
• Can you show us how your character and their feet are set up? This sounds suspiciously like overkill for your needs. Most games get by with a single trigger or spherecast or a pair of raycasts. – DMGregory Nov 20 '18 at 14:41
• Can you also edit your question to be kinda more ... descriptive? Right now it literally says that the character jumps when you try to jump, which doesn't sound like a problem that needs a solution? – TomTsagk Nov 20 '18 at 17:04

One way to address this problem is to set the radius in the call to Physics2D.OverlapCircleAll small enough that a collision is effictively checking equality. In our previous example we could reduce the radius of the circle to 1 meter, but again we must ask the question, for our purposes is 1 meter above the ground on the ground.
Another option is to add a second, hard, check ensuring that the point is within the bounds of the collider, i.e.  foreach collider in collisions { if collider.x_min <= point.x <= collider.x_max && collider.y_min <= point.y <= collider.y_max { return true; } }  Likely you will want a combination of the two because reducing the radius reduces the number of hard checks you must make.
On a side note, it may also be worth refactoring to increase readability.  void Update() { float horizontal = Input.GetAxis ("Horizontal"); rb.velocity = new Vector2 (horizontal * MoveSpeed, rb.velocity.y); if (Input.GetKeyDown (KeyCode.Space)) { if (isGrounded()) { // adjust velocity } } }