## Unity – How to correctly write the character logic "stick to the surface" that handles large and unexpected time deltas?

I've tried to pin down this problem: in a 2D platformer game (but this also applies to 3D games), I want the character "to stay" at the surface on which he is evolving and at the same time keep the same speed, no matter the tilt / decline of the current surface. Also, if the speed of my character is 1 unit / s and the current delta time is 0.25, I want him to travel exactly to 0.25 units, regardless of the "shape" of the surface encountered along the route.

Naively, I started with this basic logic (warning, incoming pseudo-code):

``````vel + = acceleration * timeDelta;
movementDelta = vel * timeDelta;

hit = raycast (pos, down);

if (hit)
{
vectorAlongSurface = hit.normal * fancyLinearAlgebra;
pos + = vectorAlongSurface.Normalized * movementDelta.Magnitude;
}
other
{
pos + = movementDelta;
}
``````

But this is only correct if my character moves along the same normal surface for the entire frame and breaks badly enough if, during the frame, the surface has different normals along the way. This may give the impression that the speed at which the character moves is inconsistent. To give you an example:

Although I expect responses of the type "do not do your pitch like this", I also consider the situations in which my game is likely to undergo an unexpected shift and to face an important time delta that could push the character to cross different normal the same frame.

Any advice on how to handle this would be much appreciated. I thought somehow to trace all the superficial tilt changes of the character's position to the point of "naïve raycast strikes", but that seems like quite expensive, especially if we consider that all the characters enemies should do the same. There must be a simple solution to what I do not see. Or maybe there is a way to make the inconsistency of speed in the given example less obvious / irrelevant?

Thanks guys.