I'm trying to create a platform game, watching videos and tutorials I see that they use the Tile Palette option very frequently.
When creating the 2D Tilemap Collider, each square has its collider, and I guess that makes it consume a lot of resources, so to solve that I create a 2D Composite collider.
The problem is when the player collides with the collider of a vertical wall, causing the player's rigidBody.velocity.y to add force and that doesn't allow me to execute the jump.
https://clipchamp.com/watch/nFKCJOv7DEX
Here you can see under the scene of the game the debug.log that ends up showing me a speed to the Y axis
This project would be for android, and I would like to optimize it as much as possible. Do you have any idea so that in that collision I do not add force Y?
I have tried to change the speed in an onColliderStay2d and put the vertical tilemaps with another tag to differentiate them from the ground, but nothing.
Instead of checking your y velocity to see if you are grounded or not I recommend just making a rectangular area under your player character and check if it overlaps with the ground using Physics2D.OverlapArea. It's way less error prone.
Also, just because composite collider creates less shapes than tilemap collider doesn't mean it is less performant, the physics system doesn't iterate them one by one. Do yourself a favor and just use the tilemap collider instead.
Related
I have a lot of problems with some 2D colliders.
In the past, they work, but now they are horrible:))
I already try with Kinematics, OnCollision2DEnter, OnTriggerEnter etc.
The objects are already on the same layer, the same position on the Z-axis, they interact with each other but my game can't detect the collisions.
The rigidbody and collider don't have material but the objects have skins enter image description hereenter image description here
I've realized that the gravity scale of your ground object is above 1. You can make it 0 so it does not get affected by gravity. Gravity Scale is on your ground object's Rigidbody2D component right below angular drag. Although I could be wrong since what kind of collisions you are expecting and your movement code, that should help resolve your problems.
I have a sphere collider on a stick and a cylinder. The cylinder has to detect a collision with the sphere collider of the stick and fire up a "OnTriggerEnter" script. Just imagine it like a drum stick hitting a drum in virtual reality.
Is there a way to avoid registering a collision on the sides of the cylinder, but only trigger on the top of the cylinder? And even without adding more protecting objects around it?
In the screenshot you see the properties of the stick and below the properties of the cylinder.
Obviously the green and red icons show which area is allowed to be triggered and which not.
Btw, simply using a thinner cylinder collider doesn't work in this case, because it wouldn't detect some collisions at all, when the stick goes too fast trough the thin cylinder. More about that in the comments.
Thank you.
Im writing a simple mini golf game in 2D. Right now im simply vertically dropping ball into the hole which has Polygon Collider of 'U' shape and is placed on tilemap that has Tilemap Collider. I want the hole collider to overlay the tilemap collider but not turning them off, because tiles and hole have different size and if im gonna shoot ball from start position and it gonna land on tile that collider is turned off because its touching hole - ball will go thru it into the depths of hell.
I have tried to put both hole_object and tilemap in different layers but it doesnt work. Even if hole is slightly over tilemap ball will stop on the ground (tilemap). Ive seen solutions like turning one collider if the second one notice collision but hole collider is like i said, in 'U' shape so tilemap collider will be always first to notice ball, which means it wont let ball go thru him into the hole.
I expect the ball_object to collide only with objects and tiles that are in the highest layers instead of all of them.
What would be the best way to adjust the height of a rididbody when for example my character goes into crouch position? would it be to get a reference to the rigid body and manually change the height etc, or would a mesh collider be a better option? Im open to any suggestions, I'm just curious what would be the best way to go about doing this.
Rigidbody itself doesn't trigger collisions, only colliders does so you should change the height of collider.
In my opinion cylinder collider is a better option then mesh collider because it produces less overhead, is easy to scale (for example by just changing height) and is quite good approximation of humanoid mesh.
So just keep a reference to cylinder collider attached to game object and changed height of collider whenever your character goes into crunch position.
This is the setup I have. I am trying to make a game in unity which appears 2d but is actually 3d. I have a simple sphere and a floor, which is made up of cubes placed next to each other(the colliders overlap a little) with the same Y value and Z value. My 2d plane is in X-Y plane(Z being the depth).
Now in the script attached to the sphere, in the Update function, I have used rigidbody.addForce() in the +ve X axis function to move the sphere forward. I have attached rigidbody to the sphere and enabled gravity. The collider of the sphere is the default one.
Now the problem is:
When I run this scene. The sphere moves forward but at the intersection of the colliders, it jumps a bit(very less but still noticable) upward and loses its momentum. It happens at every intersection.
BUT this does NOT happen if I place the sphere on a floor made up of a SINGLE cube(a very long one).
Is this problem arising because of overlapping colliders? How do I solve this issue?
Thanks in advance
I don't think this is fixable. Unity physics is approximate, and even when physics material are set up to 1.0 bounciness, momentum doesn't stay constant — I learned it the hard way when I tried to develop a game depending on constant momentum, and had to write my own little physics simulation for that.