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

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.

Related

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 Making a prefab collide with surroundings, but not other objects of the same prefab

The picture below is my simulation, and the problem im facing is that they wont collide they way i want to.
I made them move randomly and i want this behavior.
The green balls are supposed to "bounce" off the outer grey walls, so collide with them (simulate physics)
The green balls are NOT supposed to bounce of each other, but only do triggerevents (so i know when they are on top of each other)
How do i do this. I have looked at multiple tutorials and i simply dont know what to do. As far as i understand, to make a collision, one of the objects has to have a rigidbody on, and the other a normal collider?
I have to tried to follow this overview. The balls are from the same prefab, so to get a trigger for them i have to pick either
static trigger collider, rigidbody collider trigger, or kinematic rigidbody trigger collider (as seem from the overview).
BUT if i pick any of those i cant get a collision with the walls? Do i have to do the wall collisions myself?
What you can do is make all the walls static colliders, and make a script on all the balls that check whether they hit a wall or a ball. and do actions that way.
Or
Make the walls check for a collision with the ball and add a force to the negative direction that they came from or something.(up to you how you want the balls to behave)
for example :
OnTriggerEnter(collision other)
if(other.transform.tag == Ball)
//Run Some Code here
// for example
BallRb other.GetComponent<RigidBody>();
BalRb.addForce //add the force that you want.

2D box colliders are not working in Unity

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!

Capsule Collider not visible in the scene view?

I am trying to hit a ball using a golf club. My golf club has three components (three game objects: handle, shaft and head as shown in the first attached image). Head gameobject's inspector can be seen in the another attached image.
To hit the golf ball, we need a collider. So I put a Capsule Collider at head game object. But I don't see any collider at head gameobject.
The head is shown in the last image.
Why I can't put Capsule Collider at the object?
EDIT:
Thanks for the reply.
I increased the radius but still the same.
I placed a Quad in the scene and put Mesh collider onto it (called AW52Collider). Please see in the
first image. We can see the collider clearly.
But I did the same thing to Club head, as it is also a game object I should be able to put a Mesh collider. But I don't see anything. Why?
Please see in the second image.
One more thing I don't understand is when I put AW52Collider inside Club Head object, I don't see AW52Collider also. Why? Please see in the third image.
Increase the size of your collider, those values do not look like you should see the gizmos at all. Best would be to reset the collider to its default values and then play with them until you are happy.
It is best practice to create a child object with colliders on it. The reason is the placement of the collider. The collider is always centered around the pivot point of a GameObject. But you probably want to move your collider freely or disable a group of colliders manually for testing (or optimization) purposes. Also consider that an object with multiple small colliders as child objects is considered one big collider (rigidbody wise). Source (Compound Colliders)
Just enable Gizmos button and you'll find your capsule collider.
See this picture to know exactly where to find it:
(It's in the toolbar below the Scene tab)

Unity2D - Level with invisible collisions

In my Unity2D project I want that to load a level as a sprite from my resources. The game is a top down view. I then want to add invisible collision boxes where the walls in the level-image are.
How would I do that?
I have attached a box collider to my player and now? Creating an empty game object with another box collider doesnt work.
See #Savlon comment, you need rigidbody with your collider component as well for empty objects. And they will provide blocking of player object if you player object also have collider and rigidbody. Don't mark your colliders as trigger and ensure they are on the same layers.