Student query: Rigidbody passing through walls/boundaries

Hi guys,

I’m very new to Unity and although I’ve scoured many threads for a solution to my problem, none seem to be of much help! I’m creating a 2D arcade game as part of a university project which involves navigating the player up platforms at different heights.

Surrounding the game area are frames which act as a boundaries, preventing the player from going outside of it. This is where I have a problem; as the player can pass straight through these walls with ease.

My controllable character has a rigidbody and box collider attached to it, as well as a movement script that only allows the player to move horizontally, as shown below;

horMovement = Input.GetAxis("Horizontal");
		
if (horMovement) {
transform.Translate(transform.right * horMovement * Time.deltaTime * speed);

Referring to my inserted image, the frames/boundaries (shown in grey) are simply cubes with box colliders in them and nothing else besides a mesh renderer. The platforms themselves are in brown.

Right now I’m using transform.position.x = Mathf.Clamp to act as the boundary; which although does the trick, prevents my player from entering the ‘secret’ room shown in the top left hand corner of my inserted picture. To sums things up; everything shown in grey on the image is what I don’t want the player passing through!

I’m aware the CharecterController has a solution to this ‘passing through walls’ query, but have come to the conclusion that the script isn’t suitable for this particular game, purely because I can’t get it to work with other current in-game mechanics!

If anyone can help me out here, I’d be greatly appreciated!

Thanks

As a side note; if you’d like to find out more about the game i’m making, feel free to check out my production blog http://finepixelgame.wordpress.com/

If you are using a rigidbody, don’t use transform.position = … . Use Rigidbody directly instead. In your case MovePosition is probably what you are looking for.