• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by vulgarknight · Mar 10, 2013 at 10:00 PM · movementcharacterbounds

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 11, 2013 at 02:46 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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 =]

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 11, 2013 at 07:12 AM 0
Share

I'm not sure what dimensions you are looking for. Renderer.bounds is a bounding box, axis aligned, in world space.

avatar image vulgarknight · Mar 11, 2013 at 07:28 AM 0
Share

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.

avatar image
0

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. alt text

Finally, here is the completed properties, with the most important ones highlighted. alt text


screenshot-2022-05-24-201804.jpg (112.9 kB)
screenshot-2022-05-24-201548.png (86.4 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character Animation Question (Bear Force One) 1 Answer

character movement and animation 1 Answer

how to make your own character. 2 Answers

Character controller + animation of different meshes making one character 1 Answer

about character movement in 8 direction 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges