Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 12:55 AM by bpears for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by bpears · Jan 04, 2016 at 01:23 AM · c#colliderbounds

Make Collider equal to renderer bounds?

EDITED: How can I make the collider bounds equal therenderer bounds?

I am trying to make it so the box collider is the same as the renderer bounds.

I forgot to mention, Im trying to use the bounds of children as a whole, the parent object has no mesh

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

  • Sort: 
avatar image
5
Best Answer

Answer by Fattie · Jan 04, 2016 at 02:51 AM

What you're probably looking for is the Encapsulate call.

It's how you "add" bounds together. it's incredibly handy and a basic when working with 3D.

 Bounds bigBounds = renderer.bounds;
     foreach(var r in GetComponentsInChildren<Renderer>())
      {
      bigBounds.Encapsulate(r.bounds);
      }


To more or less "set the shape" of a collider, just use .size, example this or this or this or this.

("Expand" is totally unrelated. Recall that you can't set the bounds on an object, when you get the bounds of an object are just a real-time convenience calculation.)

Happy new year!

Comment
Add comment · Show 7 · 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 bpears · Jan 04, 2016 at 03:42 AM 0
Share

I tried this, and tried without children to make sure I'm not overlooking something. The collider ends up being much bigger than the mesh and gets centered away from mesh. The pivot of the object is not center but still pretty close to the object. Tried using Renderer.bounds and $$anonymous$$eshFilter.mesh.bounds. Not sure why its so huge and distant.

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEdit$$anonymous$$ode]
 
 public class ColliderToBounds : $$anonymous$$onoBehaviour {
     public BoxCollider colli;
     public GameObject bound;
     private Bounds newbound;
 
     
     void Update() {
 
         newbound = bound.GetComponent<$$anonymous$$eshFilter>().mesh.bounds;
         colli.size = newbound.size;
         colli.center = newbound.center;
     }
     
 }
avatar image bpears bpears · Jan 04, 2016 at 03:47 AM 0
Share

got a little closer in size using .extents. Think the transform of the object is throwing it off. Script is on a parent which is where object and collider are, but the bounds im getting from a child. EDIT: I think the size may be correct now its hard to tell until I can figure out how to center it ignoring the pivot point.

 colli.size = new Vector3((newbound.size.x / self.localScale.x)*2,(newbound.size.y /self.localScale.y)*2,(newbound.size.z /self.localScale.z)*2);


Tried this aswell

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEdit$$anonymous$$ode]
 
 public class ColliderToBounds : $$anonymous$$onoBehaviour {
     public BoxCollider colli;
     public GameObject bound;
     private Bounds newbound;
     //private Transform self;
     
     void Update() {
 
         newbound = bound.GetComponent<$$anonymous$$eshFilter>().mesh.bounds;
         colli.bounds.Encapsulate (newbound);
         colli.size = newbound.size;
     
     }
     
 }
avatar image Fattie bpears · Jan 04, 2016 at 01:00 PM 1
Share

hi, you just center it locally as shown in the four links,

http://answers.unity3d.com/answers/233227/view.html

does it help?

avatar image Fattie bpears · Jan 04, 2016 at 01:02 PM 1
Share

BTW I'm not sure why you are doing it in Update() ? that seems wrong

Also in the code you pasted in these comments, you do not foreach , you seem to not be doing what is in the answer?

avatar image bpears Fattie · Jan 04, 2016 at 05:49 PM 0
Share

I took a step back to make sure I could get 1 child working first and took out the child iteration. It seems like the parent objects transform is messing up the size and center once applied to itself. $$anonymous$$y Problem seems that the center is effected by all parents. And setting to Vector3.zero centers it to pivot rather than the bounds I am taking from. Edit, zero may be ok now I changed how I encapsulate from being bounds to using size of bounds ins$$anonymous$$d. But the collider is still about 10 times as big.

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEdit$$anonymous$$ode]
 
 public class ColliderToBounds : $$anonymous$$onoBehaviour {
     public BoxCollider colli;
     public GameObject bound;
     private Bounds newbound;
     //private Transform self;
     
     void Update() {
         //self = colli.transform;
         newbound = bound.GetComponent<Renderer>().bounds;
         colli.bounds.Encapsulate (newbound.size);
         colli.center = Vector3.zero;
     }
     
 }
Show more comments
avatar image
0

Answer by twobob · Apr 15, 2017 at 03:48 AM

Add a collider that matches the size of the mesh

 //In start()
 box = GetComponent<BoxCollider> ();
         if (box == null) {
             box = gameObject.AddComponent<BoxCollider> ();
             box.center = 
 new Vector3 (0, ren.bounds.size.y * gameObject.transform.localScale.reciprocal().y * 0.5f,0);
             box.size = 
 Vector3.Scale( ren.bounds.size,gameObject.transform.localScale.reciprocal())      ;
             box.isTrigger = true;
         }

Helper extension method placed after body of main class used to inversely scale the collider to the local scale

 public static class Vector3Ext {
 public static Vector3 reciprocal(this Vector3 input){
 return new Vector3(1f/input.x,1f/input.y,1f/input.z);
     }
 } 

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

Follow this Question

Answers Answers and Comments

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

Related Questions

How Does OnTriggerEnter() Work? 0 Answers

Multiple Cars not working 1 Answer

How would i find the center of an object with a Y axis offset to find the center on the top face? 1 Answer

Bounds.Contains not working properly 3 Answers

How to set parent collider equal to child collider visually? 2 Answers


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