Prevent movement off of level.

I am working in a situation where a character is moving around a board-game like level using a simple transform.translate - however, I don’t want the character to be able to move off of the edge of the level.

I want to do this by not allowing the character to never not be in contact with the collider of the level. Is there any good way for me to do this?

Or is there a way I can enact bounds to simply prevent the user from moving outside the level?

I’m trying to set up a ‘bounds’ var and use its contains function in an if statement. Hopefully it will work.

Or just add invisible “wall” colliders. An object without a renderer can still block objects physically.

My game has an “invisible fence” like that–a ring-shaped mesh collider with polys facing in, but not rendered.

I’ve tried that. Doesn’t seem to want to work. I think it’s because I’m using transform.translate to move - so I effectively ‘teleport’ past the collider.

Ah–then making sure the translate won’t happen if it puts you outside the bounds makes the most sense to me. (At least, if the level is rectangular.)

Luckily it is rectangular. I would be rather unhappy if it wasn’t.

Pretty sure this is what the CharacterController is for - to have simple movement that takes into account walls and collisions for free.

Why not upgrade to it?

-Jon

I am using Character Controller…

My issue is that I am essentially emulating a board game. The character moves a fixed distance from tile to tile. The way I found to move most easily was transform.translate. Is there a better way for me to move a fixed distance?

Here’s a sample of my code:

    if (Input.GetKeyDown ("down")){

    		
    	if (bounds.Contains(controller.center + new Vector3(0,0,-speed))){
    		transform.Translate(Vector3(0, 0, -speed), controller.transform);}

    }

‘speed’ is set to the fixed distance I move.

What behavior is that script giving you? What does bounds represent in this situation since it does not specifically reference an object? The bounds of the Mesh, the Renderer, or the Collider? It seems like you would want to get the bounds of the level by having a variable to hold a reference for the level and get the bounds from that.

I have no idea what you just said.

I barely understand what bounds does - and I’m not surprised that its not working the way I had hoped.

Any suggestions from individuals far better versed in the wonders of Unity than I?