• 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 /
This question was closed May 25, 2018 at 01:03 AM by bpears for the following reason:

The question is answered, right answer was accepted

avatar image
Question by bpears · Jan 04, 2016 at 09:34 PM · c#colliderchildboxcolliderbounds

How to set parent collider equal to child collider visually?

EDITED: Scale now works by diving parent scale. I am trying to make the parent collider conform to a selected child collider, that I assign in the inspector. However I can't quite get it's center to stay consistant. As you can see, the red parent and its blue child both have colliders. The childs own collider is correct and conforms to its own mesh, however, the parent using the script is the large collider outline seen. If I move the child, the parent collider will start surpassing its movement while I am wanting it to stay dead center of the child collider. I am trying to make the colliders match up perfectly according to child mesh.

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 
 public class ColliderToBounds : MonoBehaviour {
     public BoxCollider colli;
     public GameObject bound;
     private Bounds newbound;
     private Transform self;
         
     void Update() {
             self = colli.transform;
         newbound = bound.GetComponent<BoxCollider>().bounds;
         colli.size = new Vector3((newbound.size.x/self.localScale.x),(newbound.size.y/self.localScale.y),(newbound.size.z/self.localScale.z));
         colli.center = newbound.center - self.position;
     }
     
 }
 

alt text

asdfasdfasdf.jpg (23.4 kB)
Comment
x4000
Meri

People who like this

2 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by bpears · Jan 05, 2016 at 12:06 AM

Thank the Math Gods, I got it. This is useful for setting up a parent with a collider that will be the same as the child.

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 
 public class ColliderToBounds : MonoBehaviour {
     public BoxCollider colli;
     public GameObject bound;
     private Bounds newbound;
     private Transform self;
         
     void Update() {
         self = colli.transform;
         newbound = bound.GetComponent<BoxCollider>().bounds;
         colli.size = new Vector3((newbound.size.x/self.localScale.x),(newbound.size.y/self.localScale.y),(newbound.size.z/self.localScale.z));
         colli.center = bound.transform.localPosition;
     }
     
 }
Comment
x4000
summitgamesentertainment

People who like this

2 Show 0 · 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

Answer by Meri · Mar 29, 2017 at 03:17 PM

This will stay correct until you rotate your object, rotation will break it up, but I think this might be of some use: http://answers.unity3d.com/questions/11210/re-calculate-aabb-after-rotating-a-mesh.html

Comment

People who like this

0 Show 0 · 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

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

54 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Bounds and unwanted offset 0 Answers

How to access children's colliders. 4 Answers

Bounds with Rotation: Getting y value of the point where its on the most left side of the collider (bounds.min.x) Note that the box collider has been rotated as well. 2 Answers

Multiple Cars not working 1 Answer

Leap Motion generated hand/finger is untagged. Reason? 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