Reputation: 57
I've tried everything. The player falls through regardless of what kind of GameObject (cube etc) I place under it. It has a circle collier and a rigidBody.
How can I stop the object from falling through the floor
I should mention that, the player is supposed to die once it comes in contact with anything, so I don't know how to approach this.
Upvotes: 0
Views: 13045
Reputation: 125455
If Object is falling through the floor, here are the things to check.
1. Is Collider attached to that Object? If not then attach Collider or Collider2D to that Object.
2. Is isTrigger enabled on any of that Object Collider? If yes, then disable IsTrigger on both colliders.
3. If Rigidbody is attached to the GameObject then make sure that the player is 100% above the floor before clicking "Play" or you will experience even more problems.
4. The size of the GameObject might be small. Really small. There is a limit on what size the Object can be before it can collide with another Object. Try resizing the Object then move the camera back.
If I disable isTrigger for my player then it won't be able to go through the other objects which I need it, to go through.
This isn't a problem at-all. You can use layers to make Unity set which colliders can collider with another. Just disable the isTrigger then use:
For 2D:
Physics2D.IgnoreCollision(yourFirstCollider, yourOtherCollider, true);
or
Physics2D.IgnoreLayerCollision(yourLayer, yourOtherLayer, true)
For 3D:
Physics.IgnoreCollision(yourFirstCollider, yourOtherCollider, true)
or
Physics.IgnoreLayerCollision(yourLayer, yourOtherLayer, true);
This will let the player not go through the floor but go through any other Objects you want. You can also do this from the Editor Settings... Edit --> Project Settings --> Physics --> or Edit --> Project Settings --> Physics 2D
the player is supposed to die once it comes in contact with anything, so I don't know how to approach this
This is unrelated to the problem but OnCollisionEnter2D
is used to detect collsion. You can call Destroy
on the player.
void OnCollisionEnter2D(Collision2D collision)
{
Destroy(player);
}
EDIT:
If you also need to detect when the player touches other colliders but don't want them to actually collider then you can do what I described above and then add child Objects with collders to player. These Child Objects will have isTrigger
enabled. You can then use thee OnTriggerEnter
function to detect when there is a collision between those objects that's not a floor.
void OnTriggerEnter(Collider other)
{
}
Upvotes: 5