I have made an endless runner game. There, a cube is sliding on a slippery plane. Therefore I created multiple prefabs of planes with obstacles on it which are random placed one after another. Now, when I start the game and my player (a cube) slides on the planes, it sometimes seems to collide with the edges of a plane, which causes the cube to fly away.
private void Update(){
if (rb != null){
if (rb.position.y > 0.513f){
pos = rb.transform.position;
pos.y = 0.51f;
rb.transform.position = pos;
}
rot = rb.transform.rotation;
rot.y = 0;
rot.x = 0;
rot.z = 0;
rb.transform.rotation = rot;
if (rb.position.y > 1.01f)
forwardForce = 2500f;
else
forwardForce = 5300f;
if (Input.GetKey("d"))
rb.AddForce(sidewayForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
if (Input.GetKey("a"))
rb.AddForce(-sidewayForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
Here, rb is the Rigidbody of the cube. And as you can see, I have tried to solve the problem by detecting if the player position is too high, and then putting it just slightly above the y-position of the plane, so that caused by possible bad floating point precision, it doesn't get stuck inside the plane. Now the cube doesn't fly away, but it kind of bounces very aggressively instead. And while writing this question I found out, that you can freeze the y-coordinate of the player, which helps a bit, but instead of bouncing it slows down now, which is also bad but better.
Edit: I 'fixed' it now by just letting the cube fly slightly above the platform. By that it doesn't collide with the edges or whatever the reason was