As shown in the video, when the character controller overlaps a kinematic rigidbody, the depenetration mechanism lifts up the character controller no matter how high the collider of the rigidbody is.
This mechanism makes me unable to add any gate to my game.
Is there any workaround?
Edit:
One more video to show the problem. It was just the FPS Controller from Standard Assets.
There seems to be quite a lot of overlapping before the “jump” happens. Have you tried using Continuous Collisions? That might lessen the amount of overlap.
The movement of the soldier is hanlded by the unity built-in CharacterController by calling the move method.
Because the moving box is kinematic, so I just change transform.position of the box.
And hence the overlapping is expected to be happen.
But I don’t want the unity built-in CharacterController to be depenetrated automatically like what shown in the video.
I would rather depenetrate the CharacterController using my own script, so I would like to know whether it is possible to disable the depenetration when the CharacterController is overlapped with the collider of a rigidbody (But I still want that the CharacterController do not move into the collider of a rigidbody due to its own movement, so I can’t simply ignore the collision between them).
When you move something with transform.position, there is the possibility that the physics will see it as it is being teleported and miss out on collisions. This might explain the amount of penetration and why it moves your character on top of the box.
Try moving the box using physics instead. For example, move it with Rigidbody.MovePosition. It works about the same transform.position movement, but takes physics into account.
As I said, the penetration is expected to be happen. My main trouble is the depenetration lifts up the CharacterController no matter how high the collider of the rigidbody is.
The penetration is not expected to happen, at least not as much as happens in the video. The main cause of the problem is that the box teleports into your character and since the physics doesn’t know where the box came from it doesn’t know how to resolve the collision. If both object were moved by physics, then it would be far more information about how to resolve the collision and to push the objects apart. Also, if you use Continuous collisions the penetration will be reduced even further, which will make your collisions behave even better.
As the character movement is handled by the Character Controller, it can’t be pushed by rigidbody. So, the penetration will occur when a kinematic rigidbody moving towards it. Anyway, I just would like to know if it is possible to switch off this overlap recovery.
private int overlappingCollidersCount = 0;
private Collider[] overlappingColliders = new Collider[256];
private List<Collider> ignoredColliders = new List<Collider>(256);
private void PreCharacterControllerUpdate () {
Vector3 center = transform.TransformPoint(m_CharacterController.center);
Vector3 delta = (0.5f * m_CharacterController.height - m_CharacterController.radius) * Vector3.up;
Vector3 bottom = center - delta;
Vector3 top = bottom + delta;
overlappingCollidersCount = Physics.OverlapCapsuleNonAlloc(bottom, top, m_CharacterController.radius, overlappingColliders);
for (int i = 0; i < overlappingCollidersCount; i++) {
Collider overlappingCollider = overlappingColliders[i];
if (overlappingCollider.gameObject.isStatic) {
continue;
}
ignoredColliders.Add(overlappingCollider);
Physics.IgnoreCollision(m_CharacterController, overlappingCollider, true);
}
}
private void PostCharacterControllerUpdate () {
for (int i = 0; i < ignoredColliders.Count; i++) {
Collider ignoredCollider = ignoredColliders[i];
Physics.IgnoreCollision(m_CharacterController, ignoredCollider, false);
}
ignoredColliders.Clear();
}
I found a way to solve my problem.
The depenetration can be switched off by calling PreCharacterControllerUpdate() before and PostCharacterControllerUpdate () after m_CharacterController.Move(movement)
It seems to be fixing that problem, but can you explain why and how it works? Is there any performance hits using that code? Should I always use those methods even if the rigid bodies are far away from the player?
I am not sure if that code can cause any other problems with my other NPCs that have a NM agent + CharacterController attached?
The problem still exists unfortunately. Decided to get a custom character controller on the asset store.
This removed a lot of headaches for me. I would personally recommend Easy Character Movement
Which makes use of rigidbodies, so it still allows for pushing