- Home /
Keep character inside of an objects bounds
I am trying to restrict my character's movement to inside the bounds of another object, but I am having no luck. I want to move my character to a position inside the other object and not allow the player to leave that object's bounds. How can I do this?
Providing more information about the problem you are trying to solve will likely get you more accurate information. How to answer this question depends on how you are moving the character and what it means to be inside. All of @gribbly 's suggestions are good ones, but they all depending on the collider/mesh facing to the inside of the object. They won't work for example if you put your character inside a standard cube. And there are a few other ways to tackle this problem.
Answer by gribbly · Mar 10, 2013 at 10:22 PM
I see three main options:
1) Triggers
Add a collider to the "other" object. Make it a trigger. Attach a script to it. In that script you'll get OnTrigger* events (e.g., you'll probably be interested in OnTriggerExit).
You can move your character however you need to when you get the OnTriggerExit event.
2) Physics
Add a rigidbody and a collider to your character. Now it'll collide with any (non-trigger) colliders in the scene. Colliders can't be inside of each other, so you'll need to surround your object with a number of colliders (e.g., to cover all sides of a box you'd need six).
This way requires more setup, but you get the benefit of physics handling the movement for you (at least partly).
3) Hybrid
One interesting way to approach this would be to use triggers (option 1) to detect when you character is out-of-bounds, then dynamically spawn a physics collider positioned so that they'll bounce off it (similar to option 2, but dynamic).
That would avoid the setup overhead of creating six colliders, and would handle dynamic situations better. But getting the spawning working correctly will be painful I'd guess =]
Answer by vulgarknight · Mar 11, 2013 at 06:43 AM
Thanks, both of you. I'm going to go with the hybrid option because i want my scenarios to be dynamic and modular in that I only need to place little amounts of objects to perform tasks. I think i will instantiate a collider on 4 sides of the bounds (i restrict my characters movement on the z axis in this state already), but can you provide any info on getting the dimensions of an object in world space?
I'm not sure what dimensions you are looking for. Renderer.bounds is a bounding box, axis aligned, in world space.
I'm looking for the box that houses the character when they are meant to be contained. When the character is meant to be contained, I would like to instantiate the collider boxes on 4 sides of the box: top, bottom, left and right. I want to instantiate them literally right next to the edge of the housing box.
Answer by clkawell95 · May 25 at 01:30 AM
For anyone looking for a cleaner, modern solution to this: You can use Composite Collider 2D components. First, create an empty object and add the Box or Polygon Collider 2Ds that you need to form the desired shape.
Then, add a Composite Collider 2D component, and check the Used By Composite option on your Collider components. That will combine the meshes. Now you will have the ability to modify some new settings for your collision, including setting the geometry type of your colliders to Outline. That way they act as a 2d wall.
Finally, here is the completed properties, with the most important ones highlighted.
Your answer
Follow this Question
Related Questions
Original Blender character faces ground when not walking 2 Answers
wanna know what make my char stop 0 Answers
Create bounded movement 1 Answer
Character Controller problem 0 Answers