使用条件重新调整对象的比例

Re-sizing an object's scale with a condition

我有一个简单的立方体,我想根据以下脚本更改其 局部比例

我不希望大小变得小于一定数量,所以我放入了一个 if 循环来确保这一点。

如果用户没有指定最小刻度值,则起始刻度值被视为红线。有一个选项(复选框)来决定这个。

但是,我想知道是否有更简洁的方法来解决这个问题,而不是通过 3 个值进行如此笨重的循环?

这当然有效。当我 运行 场景并前后移动相机时,立方体 grows/shrinks 相应地并且永远不会小于起始大小(如果未勾选框)或指定的最小比例(如果方框打勾)关于这样能不能简明扼要?

/// <summary>
/// Updates the size/scale of an object as the user
/// (i.e. the hololens camera) moves back and forth.
/// This script should be attached to the game object.
/// </summary>
public class ObjectScaler : MonoBehaviour
{
    /// <summary>
    /// In the Inspector panel, drag and drop the
    /// intended object that needs to be re-sized.
    /// </summary>
    public GameObject _renderCamera;

    /// <summary>
    /// This is the minimum size that specifies
    /// the object should not get smaller than this.
    /// When you put this script on an object, it takes
    /// the actual starting size of the object, but this way,
    /// you can even specify a different size as a minimum.
    /// </summary>
    public float _minXScale;
    public float _minYScale;
    public float _minZScale;

    /// <summary>
    /// Should I use the defined minimm size, or should I assume
    /// the actual starting object size is the minimm size?
    /// If you check it, you have to provide a float for size.
    /// If you leave it unchecked, the real size of the object
    /// at start-up is taken as the minimum size.
    /// </summary>
    public bool _useTheseMinScales;

    /// <summary>
    /// The starting scale of the object at start-up;
    /// whether it is the actual object's scale or whether
    /// it is formed by _minXScale,_minYScale,_minZScale values.
    /// </summary>
    private Vector3 _startingScale;

    /// <summary>
    /// The distance between the object and holocam at start-up.
    /// </summary>
    private float _startingDistance;

    /// <summary>
    /// Current constantly-updated distance between
    /// the object and holocam at any given moment.
    /// </summary>
    private float _currentDistance;


    void Start()
    {
        var objectTransformPos = this.transform.position;
        var cameraTransformPos = _renderCamera.transform.position;
        _startingDistance = Vector3.Distance(cameraTransformPos, objectTransformPos);
        _startingScale = this.transform.localScale;
    }


    void LateUpdate()
    {
        _currentDistance = Vector3.Distance(_renderCamera.transform.position, this.transform.position);

        if (_useTheseMinScales)
        {
            if ( (_startingScale.x * (_currentDistance / _startingDistance) >= _minXScale) &&
                 (_startingScale.y * (_currentDistance / _startingDistance) >= _minYScale) &&
                 (_startingScale.z * (_currentDistance / _startingDistance) >= _minZScale) )
            {
                this.transform.localScale = _startingScale * (_currentDistance / _startingDistance);
            }
        }
        else
        {
            if ( (_startingScale.x * (_currentDistance / _startingDistance) >= _startingScale.x) && 
                 (_startingScale.y * (_currentDistance / _startingDistance) >= _startingScale.y) && 
                 (_startingScale.z * (_currentDistance / _startingDistance) >= _startingScale.z) )
            {
                this.transform.localScale = _startingScale * (_currentDistance / _startingDistance);
            }            
        }
    }
}

不看尺寸,看距离: 当 _startingDistance > _currentDistance 时,你的 x/y/zSize 只比你的 _startingScale 小。 所以解决方案如下所示:

void LateUpdate()
{
    _currentDistance = Vector3.Distance(_renderCamera.transform.position, this.transform.position);

    float scaleFactor = _currentDistance / _startingDistance;
    if (_useTheseMinScales)
    {
        if ( (_startingScale.x * scaleFactor >= _minXScale) &&
             (_startingScale.y * scaleFactor >= _minYScale) &&
             (_startingScale.z * scaleFactor >= _minZScale) )
        {
            this.transform.localScale = _startingScale * scaleFactor;
        }
    }
    else
    {
        if (_startingDistance > _currentDistance)
        {
            this.transform.localScale = _startingScale * scaleFactor;
        }            
    }
}