2D box colliders are not working in Unity - unity3d

Very simple, I created an object that simply serves as a box collider that sits over the background. When the player character runs into it, I want the character to stop, but they just walk right through it. I have both of their colliders set to 2D box colliders, neither of them have the trigger option checked, and both are rigid bodies.

My guess is that you are using Transform.position to move the player.
All rigid body objects should be moved using the rigid body specific rigidbody.velocity function. Transform.position is practically teleportation while, rigidbody.velocity actually moves the rigidbody component. Allowing for the 2d colliders to take action.
Hope this helps!

Related

The problem with the correct use of a rigid body in a 2D game

I decided to make a 2D game for the PC on Unity, where the character moves around the stage. But I have a problem with the correct use of Rigid Body 2D.
In this picture you can see my playing field.
https://i.stack.imgur.com/PBhwx.png
I want my character to move around the entire field and when colliding with objects does not go through them.
My field is Tilemap, but I hung the Box Collider on the Grid. Boxing collider surrounds the grid.
But when I use Rigid Body and turn on the game, the object falls through. And if you remove the Rigid Body, then it just goes through objects.
I tried to use different types of Rigid Body, when using kinematic and static, sprite goes through houses.
The way it works is when you attach a RigidBody2D to an object, you give it physics properties, but it won't do anything useful without a collider. If you have an object with collider attached - every other object with collider attached will collide with it.
In your case the player should have RigidBody2D and BoxCollider2D. The ground should have BoxCollider2D, and basically everything you want the player to collide with should have BoxCollider2D. So the houses should have BoxCollider2D as well.
This setup should allow the player to walk on the ground without falling through it, and collider with houses.
Make sure that the collider's area is correct. You can preview it in the editor and edit it if it's not correct.
Also if you want it to work make sure the camera points down and your game setup is alongside Y axis so if there's a rigid body object falling down it falls down on the ground not before/behind it.

In Unity 2D, how do I achieve Non-trigger collsion that doesn't "physically" move the rigidbodies?

I have a bullet game object and an enemy game object.
I want to have the ability of getting the collision point/contact, which's why I think I need the collider of the bullet to not be a trigger collider - this way, I want to have the bullet bounce off the enemy. At times, the bullet will need to bounce off of rectangular objects (not a problem), but I also want the bullet to bounce off of elliptic objects (the problem).
I want the bullet not to "physically" push the enemy, which is why, intuitively, I should set the bullet's collider to be a trigger collider.
When the bullet's collider is a trigger collider, I seemingly don't have a way to implement the bouncing.
But when the collider's not a trigger collider, it pushes the enemy object.
There's no code errors, but I think the code hasn't got anything to do with the problem.
I suspect the problem may have something to do with the Physics Collision Matrix.
EDIT
It seems that raycasting would be the solution to the problem, as Leoverload proposed. My problem became a different problem now, so I'll close off this thread, and open a new one.
Thanks for the help, #Leoverload ! :D
For the position of the hit it's very easy. You must have 2 colliders and 2 Rigidbody and then you can simply add a
Void OnTriggerEnter2D(Collision Coll) and inside it check for the tag : if(coll.compareTag("surface")) and inside you can get the position with collision.transform.position.
The collision matrix on the Physics menu only allows to remove Collision between certain layers, it's useful if you want the enemies not to push eachother!
If you don't want the player pushed you can change the collider to trigger, and destroy the bullet just after the collision with the same method as before with a void OnTriggerEnter2D and the compareTag. In this way it will look like it touches the enemy and explode, but it won't actually touch anything. If you want to add knockback to the player then you could do a simple AddForce based on the direction of the bullet and the work is done!
If you want to avoid the bullet to move walls you can set walls to static and it should work fine

Unity composite collider 2d - Collission detection with onTriggerEnter or Raycast

I have found other people asking for this on the internet but I haven´t found a solution. I have a 2d game using tilemaps and composite collider. But when I add a composite collider I can´t use ontriggerenter or raycasts to detect the ground. So I have completely removed the ability to jump because there is no way of knowing if the player is on the ground or not, resulting in the ability to jump again before landing. Has anyone found a way around this?
There shouldn't be any issues with your approach. Without code examples all I can give you is some general pointers:
OnTriggerEnter
Use OnTriggerEnter2D(Collider2D col) to detect Trigger enters in a 2D environment.
OnTriggerExit2D for leaving, OnTriggerStay2D for every frame you're still colliding with other colliders.
Don't forget that one of the objects need to have a Rigidbody2D (if you don't want to use physics, select "Kinematic" in the dropdown and check the "Use Full Kinematic Contacts". For improved collision detection, change the Collision dropdown to "Continuous".
Raycast
When using Raycasts you need to, either start the raycast from a position that doesn't include your own hitboxes, or make sure the raycast ignore its own layer.
// Raycast down ignoring Player layer
int layerMask =~ LayerMask.GetMask("Player");
RaycastHit2D hit = Physics2D.Raycast(transform.position,
Vector2.down,
layerMask: layerMask);

Raytracing not responding correctly

So I'm new to Unity and I'm sure I'm missing an easy step, but after looking online for a while for some reason I can't find the solution.
I have two objects on the screen, player and enemy. Both have Rigidbody2D, and Box Collider 2D attached. On Box Collider 2D I have clicked is triggered On Rigidbody2D for both I have clicked Is Kinematic. On player I have a simple movement script. On the enemy object I have this:
void Update () {
RaycastHit2D hit = Physics2D.Raycast(transform.localPosition,transform.right,Mathf.Infinity);
Debug.DrawRay(transform.localPosition,transform.right);
if (hit)
Debug.Log(hit.collider);
}
Now for some reason when I move player over the object if (hit)
is true, but if I move the player anywhere on the right side it is not true. What would be the reason for this? Thanks.
First of all you don't need Rigidbody for raycast detection, only colliders. Second, Physics2D.Raycast uses world position, not local, so replace "transform.localPosition" with "transform.position", this messes it up a lot if the transform is child of something. Take in mind that you are sending raycast from the right side of your transform, so maybe it's not hitting anything and values you are getting are actually correct.

OnMouseDown() doesn't work when object's collider is colliding with another collider on the mouse position

I think the title speaks for itself. I'm using 2D Colliders, and they're on the same Z position. OnMouseDown() doesn't get triggered if you're clicking another object's collider as well. This is pretty annoying. Thoughts?
At least for me it gets triggered always on one of the objects, but I cannot choose which one.
As a workaround you can add a child gameObject with 3D collider (for example Box Collider). The child is needed because both 2D and 3D colliders cannot be attached to same gameObject. Make the 3D collider expand towards camera a little bit. In that case the child gameObjects OnMouseDown() is always called.