Problem with Bounds.Encapsulate after scaling down the model

时间:2019-04-05 15:52:16

标签: c# unity3d

I have 3 sliders which are showing bounds.encapsulated values of axis X,Y,Z. You can scale down or scale up model and proportionally to Unity 3D local scale of 1 for game object.

Note: GetScale() method gets bounds.size and shows to slider.

Note: SetScale() method is called every time the value of the slider is changed and saves the game object localScale.

public Vector3 GetScale()
{
    combineBounds = _obj.transform.GetComponent<Renderer>().bounds;
    Renderer[] renderers = _obj.transform.GetComponentsInChildren<Renderer>();
    foreach (Renderer render in renderers)
    {
        combineBounds.Encapsulate(render.bounds);
    }

    Debug.Log(combineBounds.size.x);

    return combineBounds.size;
}

public void SetScale()
{
    float xAxis = 0.0f;
    float yAxis = 0.0f;
    float zAxis = 0.0f;

    if (sca.x != combineBounds.size.x )
    {
        xAxis = 1 - (sca.x / combineBounds.size.x);
        xAxis *= -1;
    }

    if (sca.y != combineBounds.size.y)
    {
        zAxis = 1 - (sca.y / combineBounds.size.y);
        zAxis *= -1;
    }

    if (sca.z != combineBounds.size.z)
    {
        yAxis = 1 - (sca.z / combineBounds.size.z);
        yAxis *= -1;
    }

    _obj.transform.localScale = new Vector3(_obj.transform.localScale.x + xAxis, 
    _obj.transform.localScale.y + zAxis, 
    _obj.transform.localScale.z + yAxis);
}

Scaling up works super fine. But scaling down doesn't work good.

0 个答案:

没有答案