Preventing collider from glitching through terrain - unity3d

I'm using a terrain in Unity 2019.2.1f1 and a custom mesh of a cave with a mesh collider.
My character that has a Capsule Collider way bigger than the entry of the cave should not be able to enter in. But due to the roundness of both colliders, he can come into force in the cave, glitching through the terrain collider.
I think it's velocity is not excessive, I'm moving the character with rb.MovePosition() in the FixedUpdate(), and I set its rigidbody collision detection to Continuous speculative (tried all the "continuous" modes)
In the animation below, you can see the mesh of the cave and the capsule collider around the character.
How can I prevent this from happening? How can I say to Unity: "I want the colliders to be rock solid and not marshmallow"?

Colliders in Unity are rock-solid. They are incapable of any kind of soft physics, and the only moment they will warp through each other is when you force them to.
Here, you are setting the rigidBody position by force, into an impossible location. The game tries its best to fit your rigidBody despite the lack of room.
You can
Only use forces, and velocities. You can simply set the velocity to the direction of your choosing, and set it to 0 when you stop moving, or use AddForce, which is basically the same thing.
Keep using MovePosition, but use a SphereCast or CapsuleCast to check if you have enough room to move first.

Related

It is impossible to implement accurate collisions in 3D SpriteRenderers? Is Billboard incompatible with rigidbodies?

I've been having difficulty implementing enemies with a billboarding system for a while:
For a new project I'm reusing some animated sprites from an old static camera shooter game I made. It consisted of shooting projectiles and hitting enemies advancing towards your position. The enemies were capsules which had an animated plane in front of them.
I wanted to go a step further and make a raycast system that would detect if the impact on a plane is on the alpha part of its texture. I achieved it using a SpriteRenderer and a plane that matched the size of the sprite which receives the impact of the Raycast.
The problem comes with the movement of the enemies. If I use Rigidbodies this conflicts with the plane which detects the impact, since I have to use a non-convex Mesh Collider for RaycastHit.textureCoord (With Convex colliders it doesn't return the correct position). I need Rigidbody based movement for the enemies to push each other when colliding.
I tried using Transform Movement, since the error does not occur, but this allows enemies to pass through each other and occupy the same space.
I started investigating navMesh but I'm not sure if it is possible to avoid collisions.
I started to place a group of trigger colliders on the enemy mimicking the shape of the sprite. But this makes it difficult for me to detect collisions when enemies do certain animations (Some enemies jump diagonally or their animations are a bit jittery).
I would like to keep the impact detection system for the sprite, but maybe there is another way to check the texture at the impact location.
I think my options are:
With rigidbody based movement: separate the animation and hit detection system from the enemy movement. And make the billboarding effect teleport to the object constantly.
With Transform movement: Detect if they are overlapping and move them in opposite direction to the overlapping to simulate the collision.
With motion based navMesh: I'm not sure if it will be useful to me, since the characters jump and I also believe that a Mesh Agent ship cannot simultaneously be an obstacle. I understand that the navMesh cannot be generated in runtime.
Leaving texture based detection and using the trigger colliders group: Animate the colliders to also follow the animations. Or simplify everything to a large collider, losing precision on impact.
-Find an alternative method to detect the impact coordinates in the texture, avoiding using RaycastHit.textureCoord and therefore Mesh Collider. (Although I would like to keep the impact detection system as it is, maybe there is a similar approach for the same result).
The reason I have not yet delved into these solutions is because I would like to keep the system simple. I'm trying to avoid separating the enemy into 2 gameobjects, simulating collisions or animating collider positions. I honestly don't know which way to go in order to move forward with the project.

How to make that 2d collider dont slide on on an inclined plane

If 2d collider with rigidbody with freezed Z rotation touches an inclined plane with its lower edge, then it begins to slide down, how to get rid of this effect? I want if rigidbody edge touch the inclined plane, rigidbody should keep his own position and dont slide down.
I agree with BugFinder, a frozen rotation on a Rigidbody will not affect the interaction between the collider and the inclined plane. You should create a new PhysicsMaterial and assign it to your Collider that is assigned to your Rigidbody. Increase either its staticFriction, dynamicFriction, or both, as well as experimenting with the frictionCombine option, which will change how the friction value of your Rigidbody will interact with the collider of the inclined plane.

OnTriggerExit collision with TerrainCollider

I'm trying to make a digging mechanism in my game by setting heights of the terrainData.
Everything works fine, but I found a bug. As can be seen on attached image, I put a Collider as a Trigger to check whether the whole terrain from the Trigger area is removed. I check it in OnTriggerExit method.
It works, but when I left some terrain 'in the middle' (as image shows) and removed terrain only around the Trigger is says that there is no collision with Terrain Collider (but it should be!!)
Thanks!
if your terrain has rigidbody in it will not detect collision because continous collision detection in terrain collider is not supported. one way to fix this is change terrain collider with box collider or remove rigidbody in terrain object.

Two Colliders overlap a little bit each other in Unity

After coding left and right movement of player which had BoxCollider and Rigidbody, An serious problem happened. when player moved toward an cube which had BoxCollider, player's collider overlapped with the cube by 0.02. Please tell me how to fix this problem.
For your information, the movement that I made was by rigidbody.MovePosition in FixedUpdate. And I already set collision detection to continuous. Also, I set the friction to zero with Physics Material.
When player don't move, the x Position is 2.525 enter image description here
But when player move, the x Position is 2.545 enter image description here
Colliders overlap - it's how collision detection works. If the overlap is too great, you can make one or both of them bigger, make the physics settings tighter, or some combination of these. Note that the amount of overlap is also affected by the speed of the objects - the more speed, the more potential overlap. What you can't do is expect PhysX to work with no collider overlap.

Unity 3d: Prevent Rigidbody from Falling Over

I have a 3d object (army man) that I added a RigidBody to. I have a Gun set up that shoots bullets at the army man --- when a bullet hits it, I want it to fall over. Now I have an issue where as soon as my game starts, my 3d object slowly falls over without any user interaction. I set X and Z freeze positions on the RigidBody but it still falls over. IF I set to freeze on Y position, my 3dObject will not fall over but then if I shoot the army man, it will just spin and not fall over. I attached a screenshot to show all my settings.
Your Collider's mesh is obviously standing on a point and is very unstable looking. Either give him a BoxCollider, or make a tall rectangle, attach a Collider to it and a RigidBody, and make the army man mesh a child of it. Remove all colliders and RigidBodies from the army man mesh.
In the rigid body you also have the option to freeze rotation around a certain axis: