Bounds calculation error

Hi,

I calculate and draw my object bounds in the following code, but obviously the result is not correct like we can see in the attached image. The extent.z is around 0.5 times of renderer size and gizmo is drawn twice with 2 different size…

	private Bounds m_Bounds;
	public Bounds MyBounds
	{
		get
		{
			return m_Bounds;
		}
		protected set
		{
			m_Bounds = value;
		}
	}

// Calculate bounds in start
	public virtual void Start()
	{
		if (rigidbody)
			rigidbody.centerOfMass = Vector3.zero;

		Renderer[] renderers = GetComponentsInChildren<Renderer>();
		Vector3 center = transform.position;
		MyBounds = new Bounds(center, Vector3.one);
		int count = renderers.Length;

		if (count > 0)
		{
			Renderer r = renderers[0];
			Bounds bounds = r.bounds;
			center = bounds.center;
			m_Bounds.Encapsulate(bounds);

			for (int i = 1; i < count; i++)
			{
				r = renderers[i];
				bounds = r.bounds;
				center = (center + bounds.center) * 0.5f;
				m_Bounds.Encapsulate(bounds);
			}
		}
	}

// Draw center and bounds :

	public void OnDrawGizmosSelected()
	{
		Vector3 center = MyBounds.center;
		Gizmos.DrawWireCube(center, MyBounds.extents);
		Gizmos.DrawLine(center + Vector3.forward, center - Vector3.forward);
		Gizmos.DrawLine(center + Vector3.up, center - Vector3.up);
		Gizmos.DrawLine(center + Vector3.right, center - Vector3.right);
	}

1198234--47771--$fail-bounds.gif

Just a guess: Read the docs

This is always half of the size.

Only half on z axis ? No.

Double gizmos is due to a second monobehaviour calling the same code. I forgave it.

Z bounds problem do not appear on an other object where center is on the -z side of object bounds, not in the middle of the game object like above.

And I forgot to say that using the accessor MyBounds.Encapsulate instead of the member m_Bounds.Encapsulate simply skip all bounds resizing.

New bounds code (don’t works better but smaller):

	public virtual void Start()
	{
		if (rigidbody)
			rigidbody.centerOfMass = Vector3.zero;

		Renderer[] renderers = GetComponentsInChildren<Renderer>();
		MyBounds = new Bounds(transform.position, Vector3.zero);
		foreach (Renderer r in renderers)
			m_Bounds.Encapsulate(r.bounds);
	}

Checked mesh vertices. Nothing wrong :

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

[ExecuteInEditMode]
class VertexHighlight : MonoBehaviour
{
	[SerializeField]
	private Bounds m_Bounds;
	public Bounds MyBounds
	{
		get
		{
			return m_Bounds;
		}
		protected set
		{
			m_Bounds = value;
		}
	}

	public void OnDrawGizmosSelected()
	{
		MeshFilter mg = GetComponent<MeshFilter>();
		Vector3[] vertices = mg.sharedMesh.vertices;
		int count = vertices.Length;

		Gizmos.color = Color.red;
		for (int i = 0; i < count; i++)
			Gizmos.DrawCube(vertices[i], Vector3.one * 0.01f);

		// bounds
		Renderer[] renderers = GetComponentsInChildren<Renderer>();
		MyBounds = new Bounds(transform.position, Vector3.zero);
		foreach (Renderer r in renderers)
			m_Bounds.Encapsulate(r.bounds);

		Gizmos.color = Color.white;
		Vector3 center = MyBounds.center;
		Gizmos.DrawWireCube(center, MyBounds.extents * 2.0f);
		Gizmos.color = Color.cyan;
		Gizmos.DrawLine(center + Vector3.forward, center - Vector3.forward);
		Gizmos.DrawLine(center + Vector3.up, center - Vector3.up);
		Gizmos.DrawLine(center + Vector3.right, center - Vector3.right);
	}
}

Dumb, there is an other mesh renderer who is not supposed to be used there…