Issue with simple collision detection

Hi all!,

Wondering if you can help me with an issue…

So I check to see if the player ‘isFalling’, but the bool is only set to false if I move on the x axis; when I’m stationary the value is true which means my player isn’t allowed to jump.

I did find a method that offsets the collider but I couldn’t quite get it to work.

Thoughts? :slight_smile:

code:

public class playerController : MonoBehaviour
{
public float jumpforce; // Variable for jump force
public float movementSpeed; // Variable for movement speed
private bool isFalling = false; // Bool to store player collider
public GameObject player;
// FixedUpdate is called after every physics update
void FixedUpdate()
{
// Ball horizontal movement
var movement = Input.GetAxis(“Horizontal”) * movementSpeed;
movement *= Time.deltaTime;
player.transform.Translate(new Vector3(movement, 0, 0));
// Makes the player jump is they are grounded
if (Input.GetKeyDown(KeyCode.Space) && isFalling == false)
{
GetComponent().AddForce(Vector3.up * jumpforce);
}
isFalling = true;
}
// Detects player collision with environment
void OnCollisionStay()
{
isFalling = false;
}
// Update is called once per frame
private void Update()
{
// Debugging for isFalling
if (isFalling == true)
{
Debug.Log(“isFalling = true”);
}
else
{
Debug.Log(“isFalling = false”);
}
}
}

Solved my own issue…

for anyone interested -

public class playerController : MonoBehaviour
{
public float jumpforce; // Variable for jump force
public float movementSpeed; // Variable for movement speed
public GameObject player;
[SerializeField]
private int jumpCounter = 0;
// FixedUpdate is called after every physics update
void FixedUpdate()
{
// Ball horizontal movement
var movement = Input.GetAxis(“Horizontal”) * movementSpeed;
movement *= Time.deltaTime;
player.transform.Translate(new Vector3(movement, 0, 0));
// Makes the player jump is they are grounded
if (Input.GetKeyDown(KeyCode.Space) && jumpCounter == 0)
{
GetComponent().AddForce(Vector3.up * jumpforce * 100f);
jumpCounter++;
}
}
void OnCollisionEnter()
{
jumpCounter = 0;
}
}