- Home /
Rigidbody player slightly phasing into wall
I have a very basic player controller script:
public class PlayerController : MonoBehaviour {
public float moveSpeed = 1;
private Rigidbody body;
void Start () {
body = gameObject.GetComponent<Rigidbody>();
}
void FixedUpdate()
{
body.transform.Translate(Input.GetAxis("Horizontal") * moveSpeed,
0,
Input.GetAxis("Vertical") * moveSpeed);
}
}
Mostly it works, but when I move the player (currently using a simple cube) into a wall (also simple blocks) and continue holding down the movement key, the player phases into the wall just a bit. When I release the movement key, they go back out to where they ought to be colliding with it. When phasing into walls, it's possible to move the player through spaces way more narrow than the character is, and sometimes to glitch through walls entirely.
I've tried using a CharacterController component instead of a Rigidbody, but then the player won't interact with physics objects and I get the inverse problem I'm having now: the player collides with the walls just a bit outside the wall, unless I shimmy back and forth, in which case it slowly slides forward until the player is actually up against the wall.
I'm stumped. This is an extremely basic and simple situation and I seem to be suffering from some part of the Unity physics engine I don't understand.
Can you confirm for us, that the collider on your object, and the collider on the walls exactly match their visible shapes? (box collider on cube, sure SOUNDS like it)
Yes, that was one of the first things checked. They're all box colliders atm, and they exactly match their visible shapes.
Answer by Vollmondum · Oct 14, 2017 at 06:47 AM
I'd not go with physics, it's lame. Instead make your wall doubled, with outer invisible shell, and let your player stick inside it, leaving visible obstacle unreached.
Answer by FlyingHighUp · Oct 14, 2017 at 04:29 AM
I'm pretty sure this is because you're moving your player with Translate(), which internally simply sets transform.position.
Setting transform.position essentially teleports your player into the wall. Then, PhysX takes over and tries to push your player out of the wall. This is typically a bad idea, because if the teleport distance is great enough the player can go right through.
Instead of Translate use rigidbody.AddForce():
rigidbody.AddForce(new Vector3(Input.GetAxis("Horizontal") * moveSpeed, 0.0f, Input.GetAxis("Vertical") * moveSpeed));
Since PhysX has an idea where the player is going, it'll do better collision tests and move the player for you. Setting rigidbody.velocity might seem like a better choice at first, but it can suffers from the same issues as Translate() does. In my experience AddForce() (begrudgingly) is really the most stable method.
It's a bit more tricky to code - especially if you're doing cartoony movement. However, the benefits of letting PhysX do more of the heavy lifting saves a lot of headaches over that pile-of-crap character collider. Past this, if your player still tunnels (typically only a problem if you're travelling at intense speeds), change the collision solving to Continuous Dynamic, and PhysX will use more expensive tracing calculations to ensure objects don't pass through each other.
Answer by TjazS · Nov 08, 2020 at 02:45 PM
I would rather go with character controller. It would not pass into a wall
Your answer
![](https://koobas.hobune.stream/wayback/20220612140944im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Box Colider not working properly 1 Answer
Box collider (with Rigidbody attached) gets stuck into another Box Collider 0 Answers
Rigid body not colliding with box collider. 1 Answer
Collision Detection for Kinematic Rigidbodies 0 Answers
Box collider gets stuck on edges of tiled floor, cube bounces up 0 Answers