Player falls halfway throufh the floor till the outer edge of collider. instead of the inner edge - unity3d

As you can see in the included video the player falls over the floortile till the outer edge of the floor. Instead it should stop at the inner edge of the tile so it looks like its standing ontop of the tile. Can anyone help? I cant find anything on the internet regarding my issue.
Im sorry if this is a easy fix. Im a beginner. This for a school project :)
Video: https://streamable.com/99jz2o
enter image description here
Photo of sprite standing on the wrong edge of the tile.
Edit: I went back to the tile palette and redrew the level. This time selecting the game object + bitmap again. Now it works. Probably the reason why it didn't work was that when I drew the level I had not added Tilemap Collider 2D and Composite Collider 2D. Now redrawing with those 2 components added it works as it should!

The problem is with your tile map collider. Instead of using this, set empty game objects as children of the objects that make up the . Then, add BoxCollider2Ds to each one, and add scale them to fit the map. If that doesn’t work, then it is either a problem with the rigidbody, or the box collider, make sure that all of the settings are set to default, then slowly change them back, and you will discover what is causing the problem. Then you can comment on this post what variable changed it.

Your issue is the Composite Collider. The docs specify that they
do not define an inherent shape. Instead, it merges the shapes of any
Box Collider 2D or Polygon Collider 2D that you set it up to use
When you attach a BoxCollider2D or PolygonCollider2D, there is a check box on them that specifies Used by Composite. When checking this box, other fields will disappear as they are now controlled by the CompositeCollider.
To use this collider, make a new object that will hold all your other colliders or make a few that hold sections of your map. Give this new parent object a CompositeCollider2D and give all the children BoxCollider2D or PolygonCollider2D. After this, make sure to check the box Used by Composite on the children Colliders.
Edit: Sorry just saw you were using a Tilemap. Just make the CompositeCollider a parent of your Tilemap while keeping the Used by Composite checked.
Edit 2: I am not too familiar with Tilemaps with Composite Colliders, but one other solution you can try before re-childing is changing the Geometry Type field on your CompositeCollider from Polygons to Outlines.

Related

How can I remove small part of collider of game object in unity 2d

click here for gif show of what I want
I want to remove mesh of object when user click on object and also remove its collider to make another object fall from that removed mesh area...
I am using unity since last month so I don't have much experience and knowledge, please help me...
Creating the destructable ground particles
One way to achieve whats shown in the gif is by creating a prefab of e.g. a circle collider that is instantiated in the area of where the dirt is in your gif. It acts as a "ground particle" and keeps the objects above itself.
You instantiate a lot of them in the area so it acts as a big collider although it is actually a whole array of smaller colliders.
Implementing the interaction logic and deactivating the ground particles
Ater that you implement the functionality of dragging the mouse over the ground particles, removing them. That is also not difficult. Shoot raycasts into the screen at the postition of the mouse (remember to use Camera.ScreenToWorldPoint) and get the collision information (confer to https://docs.unity3d.com/ScriptReference/Collider2D.Raycast.html). With the collision information you can get the reference to the instance of your ground particle(raycasthit.other.gameobject) which is then disabled through script(gameobject.setActive(false)).

Unity: opaque area of image should be a collider, transparent area should not

I'm fairly certain that I search every possible answer for this, so please forgive me if there is an answer somewhere...
I have a bunch (35) images that are procedurally assembled to make a "maze" of sorts. The "player" object is placed in a random position on the screen and I it should avoid the collidable space. The "maze" images are 2D with some opaque areas (white) and transparent areas (blue). In Unity, all of the images have Polygon Colliders that are using Composite... All of this works perfectly, except...
My issue is that Polygon Colliders use outlines or polygons, but everything in between is not a collider. That means that, unless the player hits one of the edges of the collider, it won't register an actual collision.
To better understand this, look at the image. The player will spawn in the opaque area of the image, when I want that area to be a collider.
Btw, not using Composite produces the same issue, where the player object spawns between the polygon edges.
Any idea how to make the entire opaque area a collider?
You can check for a collision within the collider by using the void OnCollisionStay2D(Collision2D collision) method for whichever object that you want to handle the test.
However, if you're trying to use the collider to determine whether an area is safe to spawn in, you might want to check whether the spawnpoint is inside the collider using collider.bounds.Contains(player.transform.position) (or whatever position you'd like to check).
Check the documentation for more info: https://docs.unity3d.com/ScriptReference/Bounds.Contains.html

Unity with Bullet Physics : Mesh Collision

I am trying to use Mesh Collision with a cube with hole model that created from Blender and export it to .fdx then import to my Unity project
Following this doc. http://bulletphysics.org/mediawiki-1.5.8/index.php/Collision_Shapes
I am using bConvexHull for this object.
I drag the mesh into the field and press Build Mess. No error.
And set BRigitBody to make it stay still.
After that i create a small red cube on top of the first object. And set BRigitBody to make it fall down to the first object.
After hit Play button, The red cube should fall down into the hole of the first object.
But it just stay on top like this
What i have missed or did it wrong?
Please give me some explanation.
Thanks.
A convex collider, by its definition, cannot have holes.
Use btBvhTriangleMeshShape instead.

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)

How to move a tile (sprite) with drag and drop in Unity 2D?

I would like to move a tile with drag and drop in Unity 2D. The tile is a sprite. The scene is an 'Unblock me' or 'Blocked in' like gameplay.
Because the tiles in real life correspond to physical objects it seemed be to a good idea to model them with colliders and rigidbody. The border of the table surrunded with invisible colliders. I hoped these will constrain the moves of the tiles realistic, when the player moves them.
Then I implemented a simple (mouse based) drag and drop behavior which is worked perfectly except the moved tile penetrates to other tiles and the border, and sometimes jumps over them. Then I learned if I am overriding physics by explicitly setting transforms position (which I do exactly in my drag and drop implementation), this will happen. OK I accept, I should set only forces, ect. on rigidbody never directly the position.
Now the question:
I am stuck here. I still want to drag and drop like user experience, and some realistic visual result. When in the real life a player moves a tile, it seems it is "glued" to its finger. How can I achieve this (ot at least similar) with just applying forces? Any suggestions or point similar existing sample/blog code?
(I know as a backup plan I can omit all the physics and constrain the tile positions by code, and create some tweens to move the tiles. Is the real solution (what I am asking for) so complicated I should vote on this backup plan?)
Edit
According to comments I've added a video:
There is nothing wrong with the way you are manipulating your dragging. The beauty of developing is being able to do things in your own way. If it works for your game, then don't fret.
Now, i recommend:
Create a new physic material. Assets > Create > Physic Material
Set your new physic material inspector settings both to 0
Attach the physic material to all your wall object colliders. This should allow for your object being dragged to move smoothly against the walls without chopping.
Do a check to see if your mouse is over another collider. If so, then stop the movement in that direction.
Since your movements seem to always be on a single axis, on collision, tell your object to snap to the edge of the wall object. You know the Wall position and scales, also you have the position and scales of the object being dragged. with that you can write a function that will offset it to the correct position when the collision occurs.
Let me know if any of that works out :P