I use OnCollisionStay and OncollisionExit to check if my player is on the ground. However, if my player goes from 1 collider, to another, without ‘‘being on no collider’’, than IsGround = false, but it should be true. I also tried to use tags but it didn’t work. Here is my script:
The white and grey cube both have a collider.
Ofcourse I can make the white cube collider longer so it overlaps the grey cube, and that works, but that is not an option for me.
So, the change between the cubes makes IsGrounded false. Is there a better way to check if the player is grounded or how can I fix it otherwise?
Hi, if you do the destroy in Update() instead of OnCollisionExit() there’s a chance you’ll have the grounded flag true from picking up the second collider.
However, you could have cases with a racing condition causing the flag to stay false.
To go arround it, you could instead store in a list all the colliders your ball touches at any given time in OnCollisionEnter, remove them from the list in OnCollisionExit, and check in Update wether you’ve got at list one ground collider in list or not.
Additionnally, this method has a major flaw that on rare occasion you wont get the OnCollisionExit, it happens sometimes when there is a lag spike and you can’t do anything to prevent it (as far as I know). So what you need to do is check if your player still collides with colliders in list, if your objects are axis aligned then it’s a really simple and efficient bounds.contains() check.
You could also consider raycasting/spherecasting to find out if your ball is grounded.
Try this instead. It is a general method I use to check for ground.
bool grounded;
void FixedUpdate(){
//whatever you are doing for movement
grounded = false;
}
void OnCollisionStay(Collision col){
if(col.contacts[0].normal.y >= 0.6f){
grounded = true;
}
}
FixedUpdate is used since it is consistently called before the collision and trigger functions have done their job. This means that the grounded boolean will have been altered, by OnCollisionStay in this case, the next time it re-enters FixedUpdate.
The if condition inside of OnCollisionStay checks for the angle of what is being collided with and makes sure it is below a certain angle of elevation.
Worrisome, you did not have the know-how to correct yourself. In anycase, OnCollisionEnter, OnCollisionExit, and OnCollisionStay take a Collision as the parameter inside the bracket, not a Collider which was my mistake, and has been changed accordingly in my original reply.
The reason I like this approach is because with the proper setup you can get rid of OnCollisionEnter and Exit and replace it with OnTriggerEnter and OnTriggerExit which don’t allocate memory, it’s probably not a concern for you just yet so don’t worry too much about it though.
if (grounded == false)
{
Destroy(GameObject.FindWithTag("Player"));
}
under OnCollisionStay, nothing happend.
If I put it under Update, the player instantly destroys.
With grounded I don’t mean going up, but falling down, so falling from the cube.
Line 31 is where you declared your touchingColliders list so it can be only used within that method.
In order to use this list/variable in whole script declare it outside of the method - like you did with the other variables.
Yes, I still have to learn a lot. I don’t see the problem of the error sometimes.
Out of nowhere I came up in one of the most simple idea that easily fix my problem. I don’t know how I didn’t came up with this earlier.
This is all what I needed :3