I have a player that uses a Character Controller, and a modified version of the SidescrollControl. I want to restrict the Z movement. When I do this by simply setting it’s transform z property to 0 on an update, it works until it runs into a collider (lets say a wall). If you keep trying to move into it, it will “break” right through in a fraction of a second and continue on its way. The same thing happens if I try and use a Configurable Joint instead and restrict the Z through there. Does anyone have any idea how to fix this? I feel like there is something obvious I am missing. I’ve searched and searched and can’t find any other people having this issue. Thanks.
I’m going to guess that setting Z is teleporting it into the wall just far enough that it decides you need to move freely to get unstuck. Say you hit a sphere and slide a little forwards and sideways on it. z=0 snaps it to inside. Same with a slightly crooked wall. Best solution is to lock z-position, but charControllers can’t do that.
A brute force hack would be to slap two giant colliders at z=1 and z=-1, and let them keep z around 0 (give a really slippery physics material?)
You could add z movement in the script (moveDirection.z = -transform.position.z) to move it back to 0 next frame, if it gets off.
You might also try having collisions go a tiny bit backwards (transform.position -= moveDirection*0.05 ?) to be sure setting z=0 won’t put you inside it.
try this
function LateUpdate(){
if(transform.position.z > 0)
{
transform.position.z = 0;
}
}