Detect a kinematic rigidbody collision with a static rigidbody using Collider2D.IsTouchingLayers - c#

I have a tilemap called Moving Platform and attached to it is a kinematic rigidbody (with layer mask as "Ground"). Further, there is a foreground tilemap with a layermask "Ground" (and static rigidbody) assigned to it. The scene arrangement is shown in the screenshot below:
Components attached to the platform are shown below:
Components attached to the foreground are shown below:
The highlighted platform moves to right and collides with the foreground. I am trying to detect this collision in my Update() function, but the debug log always says false
Debug.Log(myCollider.IsTouchingLayers(LayerMask.NameToLayer("Ground")));
How do I detect the collision between the two tilemaps?
If I replace the "Ground" by "Player" the collision is getting detected, since player has dynamic rigidbody attached to it. It doesn't work even if I make the platform collider a trigger collider or made the platform as dynamic rigidbody. OnCollisionEnter2D isn't working either.

This is a few years old but I found myself having the exact same problem. The fix I found is to check the box labelled "Use Full Kinematic Collisions" on the Kinematic Rigidbody. This will allow Kinematic/Kinematic and Kinematic/Static collisions, which is what we're looking for!

Related

How to make Trigger enter working in unity?

i'm trying to make character to lose 1 hp when it hits enemys trigger, but it's not working
I'm using OnTriggerEnter2D and debugging triggers name, but there's nothing in logs.
Enemy
Character
Character Controller script
Add a Rigidbody2D component to your Enemy and set it to IsKinematic. Also, it doesn't look like your Character has a collider attached to it so add a BoxCollider2D to the Character, too (unless it has one, I can't see all of its components).
Objects that have trigger colliders attached also need a rigidbody component even if they aren't physics objects. Making the rigidbody kinematic will ignore the physics system but allow the collision to be detected.

Use IsTouching() in a trigger collider

I have this code for destroying an enemy when player touches it:
It works when the collider has isTrigger set to false. I've read the documentation and I think it's because it uses the physics engine to check whether player is touching or not.
Is there an easy way to avoid this, I like the cleanliness of isTouching and I need it to have a trigger collider so it doesn't affect the player movement.
If there isn't, is there a way to use the collider I already have to check for the collision with player?
I did it with OnTriggerEnter2D as jmalenfant suggested.

Collision between Box Collider 2D and Polygon Collider 2D not working

Currently I am working on a networked 2d platform game.
My player is an empty object which serves as a parent to my actual graphics etc. The empty object has a rigidbody (3d) attached to it as it needs to have one in order to use the configurable joint component ( note: I move my player around using this 3d rigid body).The player body however, a child to the graphics, has a box collider 2d attached to it as it after all it is a 2d game and has to collide with other 2d objects.
My platforms have polygon collider 2ds attached to them (which is connected to a platform effector if that is worth mentioning).
Now, even though both my player body (which is a child to graphics which is a child to my player object) and my platforms have 2d colliders on them they do not collide and can simply go inside each other (neither of them is marked as isTrigger).
In order to solve this I thought I would add a rigidbody 2D to the player body and see if that would do anything. Now upon adding the rigidbody 2D collisions did work but as soon as I made the rigidbody 2D have all position and rotation constraints ticked or as soon as I made it kinematic or static it would cease to collide with my platforms. The problem is I need to have the rigidbody 2D be static or not be able to move as I am currently moving my player object using the rigidbody (3d) attached to it and do not want any additional movement of the player body upon colliding with objects.
I know this is quite a lot of information, so if you have any questions or would like further information just comment and I will be quick to respond. Thank you :-)!
Edit:
2d ray casts are also unable to hit the player body box collider 2D
Edit 2:
So to recap:
If either the player body or the platform has a rigidbody 2d that is not static (dynamic) and can move collisions work. However currently I only have a rigidbody 2D on my player body which has to be static though (as explained earlier) as well as a box collider 2d. My platform(s) on the other hand currently only has a polygon collider 2d as I do not see why it would need a rigidbody 2D.
A static collider won't detect the collision with another collider if it doesn't have a rigidbody, if it is static or if it has a kinematic rigidbody. At least one of your two objects needs to not be static and to have a rigidbody which is not kinematic to be correctly detected. So you do need to add a Rigidbody2D to your platform.
When you have a doubt about why a collider wouldn't collide with another one, always refer to this page of Unity's documentation which sums up which kind of collider will collide with another one.
Rigidbody2D can't collide with Rigidbody3D, here's a workaround though
http://answers.unity3d.com/questions/580769/can-rigidbody-2d-collide-with-3d-colliders.html

Why don't I get a collision?

I have a cube tagged "player", which needs to jump at certain locations, represented by Empty GameObjects.
I want to make a point system and that why I put on some of the places other game objects tagged "Coin"
The cube has an animation which plays each time it moves to another point recognized by clicking with the mouse on it.
When I jump on the coin location I want the Coin object to get destroyed.
I use this function:
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Coin") {
Debug.Log ("Plm");
stroy (col.gameObject);
}
The problem is that when I'm jumping at the Coin location nothing happens.
I have box colliders on both objects and OnTrigger checked on both.
1) Make sure one gameobject has a rigidbody
To have the OnTriggerEnter happen you need at least one of the two object to have a RigidBody attached to it.
2) Static Trigger Collider
An object that has a collider and isTrigger checked but no rigibBody is considered a Static Trigger Collider which are From Unity Documentation:
This is a GameObject that has a Collider but no Rigidbody. Static colliders are used for level geometry which always stays at the same place and never moves around. Incoming rigidbody objects will collide with the static collider but will not move it.
This type of collider interactions is perfect for your coins because they do not move and stay static.
3) Rigidbody Trigger Collider
The next type of collider interactions is a Rigidbody Trigger Collider which are :
This is a GameObject with a Collider and a normal, non-kinematic Rigidbody attached. Rigidbody colliders are fully simulated by the physics engine and can react to collisions and forces applied from a script. They can collide with other objects (including static colliders)
This type of collider is what you need on your player and as it says in the Unity Documentation it will be able to collide with the Static trigger collider.
Important observation:
Having IsTrigger set to true on your player collider will cause your player to not respond as a solid object
A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through.
which might not be the most optimal solution for you because most of the time you want your player to be able to collide with other things like the ground. So you can set the isTrigger to false and just have a rigidbody on you player and it will react to the coins if they're set as Static Trigger Collider.
If you want an easy way to know what can collide with what Read the colliders documentation and take a look at this chart that shows it in a more simpler way.

Making a gameObject push the player

I am working on a Unity 3D game for Oculus and I have problems with making my objects to apply physics on a player. So getting rid of a CharacterController and using something like a rag doll is not an option.
I am using OVRPlayerController, that has a Rigidbody with mass 1 and a box collider on it. My gameObject has a Rigidbody of mass 100, and a box collider. But when the object hits the player it just goes through it, whereas I want it to push the player in x direction.
I tried using onColliderHit but it doesn't even recognize the collision between the player and the object, so I checked box collider on the object to be a trigger and I use OnTriggerEnter() to recognize the collision.
I tried to translate the player's position on collision, but player gets positioned to weird places out of my map for some reason. Here is what I use:
info.transform.Translate(new Vector3( -0.5f, 0.0f, 0.0f));
info.transform.rotation = Quaternion.identity;
I also tried to manually set the x position of the player but this doesn't work, and I know I am not supposed to do it.
I searched for answers for a long long time, so please don't answer to this with something like "oh, have you tried googling it, there are a lot of similar questions" etc.
It should be detecting the collision just fine, as long as one of the box colliders has a non-kinematic (Is Kinematic = false) rigidbody attached.
Make sure Is Trigger is false, and you specified a material for your collider.
Also try messing with the other properties on the rigidbody, such as Collision Detection and Mass. The unity docs indicate that your mass should be no more or less than 100 times that of other rigidbodies.

Categories

Resources