I'm working on a fairly robust game with another developer and we've been having a really serious issue with (I believe) the character controller. Specifically, if there's ever a large lag spike (as there are somewhat frequently - the code's not well optimized in some parts) there's a chance that the character controller will wing right through all nearby colliders and fall out of bounds.

It happens pseudo randomly, but I've noticed that it seems to be somewhat predictable, such as standing next to certain objects (nothing weird about them - just tables with colliders).

The lag often is a cause of large chunks of a level having SetActive(true) called on them in one frame or multiple objects being instantiated in one frame. These objects sometimes have colliders/rigidbodies attached but don't seem to be in any direct contact to the character controller.

I can very consistently make it happen by moving towards certain walls (again, very normal walls) and alt-tabbing while my character is still moving also seems to cause the issue predictably, which is VERY strange.

Has anyone ever experienced anything like this with the character controller? I'd give a code sample but honestly not even sure where from the codebase would be pertinent other than

if (Time.deltaTime < 0.15f)
    if (applyGravity && noClip == false)
        moveDirection.y -= gravity * Time.deltaTime;

    controller.Move(moveDirection * Time.deltaTime);

Which seems awfully straightforward to me. Obviously moveDirection gets changed elsewhere in the function too but it's all similarly bland. I'm pretty experienced with this sorta' stuff and it doesn't ring wrong to me at all.

Some other stuff.

Checked that moveDirection isn't a huge number

Tried eliminating Time.deltaTime from the equation

Fairly sure colliders aren't ending up inside of the player while a lag spike happens (again, alt-tabbing will do it too)

Geometry surrounding the player is clean (box colliders, mesh colliders on well-developed meshes)

Nothing strange like rapidly setting any components on or off is happening to my knowledge.

Anyone have any experience with this sort of issue?

  • 1
    \$\begingroup\$ Have you considered doing your movement in FixedUpdate so you never experience longer than normal simulation ticks? This will tend to improve the consistency of your gameplay by decoupling it from your rendering framerate. \$\endgroup\$ – DMGregory May 16 '19 at 16:10
  • \$\begingroup\$ Sounds to me like your getting a large deltaTime. If your movement is too large the physics engine might not trigger properly since it looks like you essentially teleported over an object. Fixed update could work, but in general it seems like you need to make sure you cap your movement per simulation step. \$\endgroup\$ – user2927848 May 16 '19 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.