• 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
0
Question by neosca · May 28, 2017 at 01:44 PM · positionboundsclamptransform.translateshape

How to clamp gameobject movement in a irregular shape

Hello,

I am using a runtime gizmo to translate gameobjects in my game. I need to know how to clamp the objects movement in an irregular shape.

For example: If the shape is a Box. alt text

I can calculate the Bounds of this shape and using the Bounds.max.x, Bounds.min.x, Bounds.max.y, Bounds.min.y, Bounds.max.z, Bounds.min.z and clamp the position like this:

Vector3 pos = gameObject.transform.position;

pos.x = Mathf.Clamp(pos.x,Bounds.min.x,Bounds.max.x);

pos.y = Mathf.Clamp(pos.y,Bounds.min.y,Bounds.max.y);

pos.x = Mathf.Clamp(pos.z,Bounds.min.z,Bounds.max.z);

gameObject.transform.position = pos;

This is all good till it's a box or a rectangular shape, because I can use either Renderer.Bounds or Collider.Bounds, However, if it is a irregular shape like this:

alt text

And, I calculate Bounds value, it will be incorrect since Bounding box/bounds will always be calculated in a square/box shape and will give incorrect max and min value, making the gameObject go outside the shape.

I am using the below RuntimeGizmo to translate my gameObject. https://github.com/HiddenMonk/Unity3DRuntimeTransformGizmo

p.s. The Shape in my case is a square shape and a L-shape room, made by using primitive cubes acting as walls. gameObject in question is a piece of furniture.

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 craftercis · Oct 10, 2018 at 08:59 AM 0
Share

Do you have an solution for this problem? I have the same problem now

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BastianUrbach · Oct 12, 2018 at 04:00 PM

First of all, you can do it for all convex colliders (all primitive colliders as well as mesh colliders marked as convex) like this:

transform.position = someCollider.ClosestPoint(transform.position);
Unfortunately that doesn't allow for an L-shape because that would be concave. However, you can build concave shapes from several convex shapes (for example an L-shape from two boxes). Then you just need to select the one that gives you the closest point:

public Collider[] shapes;
void Update () {
    Vector3 bestPoint = transform.position;
    float bestDistance = float.MaxValue;
    foreach (var c in shapes) {
        Vector3 currentPoint = c.ClosestPoint(transform.position);
        float currentDistance = Vector3.Distance(currentPoint, transform.position);
        if (currentDistance < bestDistance) {
            bestDistance = currentDistance;
            bestPoint = currentPoint;
        }
    }
    transform.position = bestPoint;
}
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to predict position of object before transform.translate will apply? 1 Answer

Clamping "X" Position Movement On Platform (Code Inside) 2 Answers

How do I clamp the camera to the boundaries of a game object I clicked on? 1 Answer

manipulating object's position without RigidBody2D component 0 Answers

Please help finding center of object 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