• 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 Jon_M · Apr 17, 2012 at 07:07 PM · collisionphysicsrigidbodycolliderscale

Detecting When Intersection Penalty is Being Applied

So I'm making a game where the size of certain objects with rigidbodies is scalable. Unfortunately, there's nothing stopping me from upscaling them into a place where they won't fit. When that happens, Physics applies a penalty force to push them out of an intersection. I'd like to stop the scaling when that happens, but I've found no way to check it.

It seems like there should be a way to check if that penalty force has triggered in the last frame. Does anyone know how to access that or otherwise detect if two objects are intersecting (not just colliding)?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ferro · Apr 17, 2012 at 09:58 PM

If your collider is sphere or capsule there are methods to check intersection.

 Physics.CheckCapsule
 Physics.CheckSphere

methods does that.

Or, maybe you can make rigidbody of your objects kinematic and scale them up until OnCollisionEnter method is triggered.

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 Lttldude · Apr 17, 2012 at 10:19 PM

If I understand correctly, you just want the scaling to stop when it gets too big and like hits a wall or some collider/rigidbody, but still be able to go back down

I think you could use OnCollisionEnter or OnTriggerEnter. Basically, when it collides with something, save the current scale vector3 and enable the stopscaling portion of script in the update function, thus making it so the scale is locked at that maxScale vector3. When it exits (not colliding), it will disable the stopscaling script and allow scaling to resume.

Here is an example with OnCollisionEnter:

     private var stopScale = false;
     private var maxScale : Vector3;
     
     function OnCollisionEnter(collision : Collision)
     {
        if(collision.gameObject.tag == "wall") //you don't need this conditional statement if you want it to stop scaling when it hits anything, this just allows you to be specific with what stops it's scale
        {
           maxScale = transform.lossyScale;
           stopScale = true;       
        }
     }
     
     function OnCollisionExit(collision : Collision)
     {
        stopScale = false;
     }
     
     function Update()
     {
        if(stopScale)
        {
            transform.lossyScale.x = Mathf.Clamp(transform.lossyScale.x, 0, maxScale.x);
            transform.lossyScale.y = Mathf.Clamp(transform.lossyScale.y, 0, maxScale.y);
            transform.lossyScale.z = Mathf.Clamp(transform.lossyScale.z, 0, maxScale.z);
     }

I haven't tested this script so there may be some bugs, but get back with me if it doesn't work. Good Luck.

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 Jon_M · Apr 18, 2012 at 03:30 PM 0
Share

Unfortunately, it isn't quite that simple. A lot of these objects are touching the ground or a single wall and I want that to still work. What I don't want to work is if an object is "pinched" between two non-rigidbody objects. When that happens, the penalty force gets applied from both sides and makes the object 8 different kinds of jittery.

I've managed to get some results from SweepTests in cardinal directions, but it isn't perfect and doesn't seem to work for mesh colliders or objects with multiple colliders. Also, SweepTests don't seem to work if the object is already touching something, just if it's about to. Also, it doesn't differentiate between objects with rigidbodies (which would get pushed out of the way) and objects without them.

There has to be something in Unity's physics system that detects when to trigger a penalty force. Unfortunately, the engine hides some things from developers and I'm worried this might be another thing like that.

avatar image Lttldude · Apr 18, 2012 at 10:01 PM 0
Share

Well you could check when it is in collision with two colliders/rigid bodies by using OnCollisionStay, which changes a boolean to true, and when tehy exit it goes back to false. Then in the update function if both booleans are true, then you stop the scale. $$anonymous$$ake sense? I apologize if it doens't.

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

6 People are following this question.

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

Related Questions

Camera gets flung off of the map when the player collides with certain objects. 0 Answers

How do i get OnTriggerStay2D to work? 1 Answer

Collision causes weird effects 2 Answers

Rigidbody necessary to use collision detection 0 Answers

OnCollisionEnter but have colliding object not move the object it collides with 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