Prevent pass through the wall (Character)

Hello, I have a third person survival game. I have a problem and not solve it. my chracter pass the inside the wall. How can i prevent this ?

This is my character and you can see inside the wall(If ı move the character, character pass the wall). My character has collider and rigidbody.

This is my sample wall. I try add mesh collider and box collider but not working. Why?
I do not want to pass through the wall when character move. Thank you.

A Box collider would be good for the wall. Mesh collider’s not needed.

What is the code you’re using to move the character? For a rigidbody and a collider, you should be moving with the physics system.

Another option you could try is using the character controller instead: Unity - Manual: Character Controller component reference

1 Like

This script is nside the player script.

void Move(){

        float moveSpeed = runSpeed;

        if (playerInput.IsWalking)
            moveSpeed = walkSpeed;

        if (playerInput.IsCrouched)
            moveSpeed = crouchSpeed;

        Vector2 direction = new Vector2 (playerInput.Vertical * moveSpeed, playerInput.Horizontal * moveSpeed);

        if (direction != Vector2.zero)
            footSteps.Play ();
        if(playerHealth.currentHealth > 0)
        MoveController.Move (direction);

    }

And this is the my movecontroller script.

public class MoveController : MonoBehaviour {

    public void Move(Vector2 direction){
        transform.position += transform.forward * direction.x * Time.deltaTime +
            transform.right * direction.y * Time.deltaTime;
    }
}

Right, so you are not moving with the physics system.
One other option I didn’t mention before is that you could raycast to determine valid movements.
So, options are: raycasting to determine valid moves, physics system to move, or using a character controller.

When you change the transform’s position in code directly, it doesn’t obey collisions automatically, as you imagined.

Actually, I didnt understand. Can you explain more? How can İ fixed the code?

If you were stumped by all of that, my best advice to you is to follow some tutorials so you can be better acquainted with Unity: https://unity3d.com/learn/beginner-tutorials

Start there, and expand on things from there! :slight_smile: