The character is stopped by the colliders in the walls but if I continue to walk towards an corner, it goes through the colliders.
This is what is happening:
The character’s Rigidbody
and Capsule Collider
:
Both walls have Box Collider
s within them.
The code that governs the player walk is this:
void FixedUpdate()
{
movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
_rigidBody.MovePosition(_rigidBody.position + movement * Velocity * Time.deltaTime); //Velocity = 10
...
I know it’s been a while, but I ran into this problem and couldn’t any solutions online.
If you are controlling movement by changing transform.position
manually, you allow your character to ever so slightly clip into a wall by running into it.
This is not a huge problem on its own, but if you then run diagonally into a corner it will push your character fully through the wall.
I simply changed my implementation to change RigidBody.velocity
.
New implementation:
gameObject.GetComponent<Rigidbody>().velocity = MoveSpeed * movement.normalized;
Old implementation:
transform.position += (MoveSpeed * Time.fixedDeltaTime * movement);
That solved it for me.