Manually write "bounce back" of player controller.

My player controller uses raycast to keep constant height, it uses my own gravity, inertia etc implementation.
So far i delayed thinking about how would i work with walls collision detection.
It’s more of a pseudocode discussion here, how would i do that?

Should i store the position and return to it if i hit the wall?
That’s currently the only thing that comest to mind but it has some issues, like player will feel
rubberbanding, jumping back in time.
I cant use rigidbody but i can use colliders.

Or maybe i could activate rigidbody when player collides with wall?
But again numerous problems occur, its almost impossible to control rigidbody die to my controller.

There are two basic approaches:

  1. Using raycasts to see how far the player could go in the direction he is heading. If the ray is longer or equal his velocity, then you can move him as usual. If the ray comes back shorter than how far he’d move in a tick, he is blocked and you have to calculate the position based on that.

  2. Iterate over the velocity. For example, in a 2D game, you could iterate over the x-component first, and then over the y-component of the velocity vector. During each iteration, you would check if the player would collide with a solid if you moved it. If it collides, you break the loop. If not, you move the player and iterate again. Repeat until you moved how far it should move, or until it hits a solid.

1 Like

Thank you very much.
First approach is kinda hard because objects can have holes/be different heights so you will need a lot of raycasting in many directions which is not good for performance (i think, i don’t know havent tested).
The second one seems like exactly what i need.
Once you collide the direction is blocked.
Simple and beautiful, thanks.

What i can do is attach say 8 colliders, radially and if one is hit block corresponding direction.