0

So I created a plane to be my floor in my unity project. I also assigned that with a mesh collider and a box collider and set it with convex but not as trigger. I then created an item with also a mesh and sphere collider and set it to use gravity, since I wanted the item to fall onto the floor. But when I start the game, it still falling past the floor. I have tried to find a solution already in YT, here and other places, but the only ideas they give is assigning a collider. This doesn't work as for me. I also tried to fill the colliders with materials but cannot assign anything with it. I also tried to write a code, but this does not help either.

Does anyone have an idea how to solve it?

public class IsGrounded : MonoBehaviour
{
Vector3 velocity;
public float gravity = -9.81f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
bool isGrounded;
// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    if (isGrounded && velocity.y < 0)
    {
        velocity.y = -2f;
    }
}
}
Leo
  • 9
  • 3
  • 1
    Assuming you've done everything correctly, did you check your Physics collision Matrix? Are the Layers set to effect each other? – jiveturkey Jan 18 '22 at 17:16
  • Your plane should probably just use one collider, box and mesh are a bit overkill. I assume your object has a rigidbody, so you could say it has gravity. If you want your IsGrounded.cs script to work, you still need to assign velocity to your rigidbody and increase it first if it's not grounded, or at least do something with that velocity. – Juri Knauth Jan 18 '22 at 18:28
  • @jiveturkey If you mean in the settings in physics, there is everything enabled. – Leo Jan 18 '22 at 20:45
  • @Juri Knauth Yes my object has a rigid body and gravity is enabled. So you saying that I need to change if (is grounded && velocity.y < 0) to a (! is grounded...? and then when Ii is grounded to change value to 0? – Leo Jan 18 '22 at 20:53

1 Answers1

0

So finally, I manage to find the solution from a helpful yt video. What I needed to change was to deactivate both, the floor and the items, the trigger event. Since Unity does not allow you to use without kinematic and convex, I just clicked on convex and nothing changes. Now, items will not fall down the plane (still can happen, like when I throw the item down, it will sometimes go through).

Leo
  • 9
  • 3