- Home /
Bounds and unwanted offset
Hi,
I'm having issues with Bounds.Encapsulate
. I'm trying to update the BoxCollider
of my empty GameObject
depending on its children.
When I let the position of the empty gameObject to 0, 0, 0 the BoxCollider is properly displayed and encapsulate all of its children.
But when I move the empty object, I have an unwanted offset that appears and I don't know how I can get rid of it.
I searched for hours but found nothing...
Could you help me please ?
Thanks !
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoundsTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
foreach (var r in gameObject.GetComponentsInChildren<Renderer>())
{
bounds.Encapsulate(r.bounds);
}
BoxCollider box = GetComponent<BoxCollider>();
box.center = bounds.center - transform.position;
box.size = transform.InverseTransformPoint( bounds.size);
}
}
}
When position is untouched all is ok:
But when I move the empty object, I have spaces on the y axis (top and bottom):
Your answer
Follow this Question
Related Questions
How to set parent collider equal to child collider visually? 2 Answers
Mesh collider not working as expected 2 Answers
Get EXACT SkinnedMeshRenderer bounds 2 Answers
How Does OnTriggerEnter() Work? 0 Answers