- Home /
The question is answered, right answer was accepted
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
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!
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;
}
}
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;
}
}
hi, you just center it locally as shown in the four links,
http://answers.unity3d.com/answers/233227/view.html
does it help?
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?
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;
}
}
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);
}
}
Follow this Question
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