- Home /
The question is answered, right answer was accepted
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;
}
}
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;
}
}
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
Follow this Question
Related Questions
How to access children's colliders. 4 Answers
Distribute terrain in zones 3 Answers
Leap Motion generated hand/finger is untagged. Reason? 1 Answer