简体   繁体   English

如何判断对象是按比例放大还是按比例缩小?

[英]How can I tell if the object is scaled up or scaled down?

Not starting but finished scaling up or down. 没有开始,但完成了放大或缩小。

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

public class Scaling : UnityEngine.MonoBehaviour
{
    public GameObject objectToScale;
    public GameObject lookAtTarget;
    public float duration = 1f;
    public Vector3 minSize;
    public Vector3 maxSize;
    public bool scaleUp = false;
    public Coroutine scaleCoroutine;

    public void Inits()
    {
        objectToScale.transform.localScale = minSize;
    }

    public IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration, Camera objectToScaleCamera)
    {
        float counter = 0;
        Vector3 startScaleSize = targetObj.transform.localScale;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);

            if (scaleUp)
            {
                var lookPos = lookAtTarget.transform.position - objectToScale.transform.position;
                lookPos.y = 0;
                var rotation = Quaternion.LookRotation(lookPos);
                objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, counter / duration);
            }
            else
            {
                var lookPos = lookAtTarget.transform.position - objectToScale.transform.position;
                lookPos.y = 0;
                var rotation = Quaternion.LookRotation(objectToScaleCamera.transform.forward);//SwitchCameras.GetCurrentCamera().transform.forward);//Camera.main.transform.forward);
                objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, counter / duration);
            }

            yield return null;
        }
    }

    public IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration, float rotationSpeed)
    {
        float counter = 0;
        Vector3 startScaleSize = targetObj.transform.localScale;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);

            targetObj.transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime, Space.Self);

            yield return null;
        }
    }
}

I want to be able to know the state of the object if it's scaled up or scaled down. 我希望能够知道对象的状态是按比例放大还是按比例缩小。 Maybe using a public static bool ? 也许使用公共静态布尔?

So in other script I will be able to check for example if(Scaling.scaledUp == true) then I know the object finished scaling up. 因此,在其他脚本中,我将能够检查例如if(Scaling.scaledUp == true),然后我知道对象已完成放大。 Same for down. 下来也一样。

So, you want to check if objectToScale.transform.localScale <= minSize or if objectToScale.transform.localScale >= maxSize? 因此,您想检查objectToScale.transform.localScale <= minSize还是objectToScale.transform.localScale> = maxSize? All of those are public variables, so a standard 'if' statement should do the trick, but you can also make those inline properties to increase readability. 所有这些都是公共变量,因此标准的“ if”语句应该可以解决问题,但是您也可以使这些内联属性提高可读性。 As long as the scale is uniform, you can use any of the three axes to do the checks. 只要比例尺是均匀的,就可以使用三个轴中的任何一个进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM