简体   繁体   English

如何自动缩小对象?

[英]How can I scale the object up down automatic?

This will scale up down smooth when pressing on G key. 按下G键时,这将平滑缩小。 But if I want it to scale up down automatic without pressing any key how can I do it ? 但是,如果我希望它在不按任何键的情况下自动按比例缩小,该怎么办?

I want it will scale up down nonstop. 我希望它会不断缩小。 Using a bool flag to decide if it's in automatic mode or key press mode. 使用布尔标志来确定它是处于自动模式还是按键模式。

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

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

    private void Start()
    {
        objectToScale.transform.localScale = minSize;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.G))
        {
            scaleUp = !scaleUp;

            if (scaleCoroutine != null)
                StopCoroutine(scaleCoroutine);

            if (scaleUp)
            {
                scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, maxSize, duration));
            }

            else
            {
                scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, minSize, duration));
            }
        }
    }

    private IEnumerator ScaleOverTime(GameObject targetObj,
        Vector3 toScale, float duration)
    {
        float counter = 0;
        Vector3 startScaleSize = targetObj.transform.localScale;

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

            yield return null;
        }
    }
}

Add a boolean 'isInAutomaticMode'? 添加布尔值“ isInAutomaticMode”?

private void Update()
{
    if (Input.GetKeyDown(KeyCode.G) || isInAutomaticMode)
    {
        scaleUp = !scaleUp;
        // ...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

    public bool automatic = false;
    public bool coroutineIsRunning = false;

    private void Start()
    {
        objectToScale.transform.localScale = minSize;
    }

    private void Update()
    {
        if(automatic)
        {
            if(!coroutineIsRunning)
            {
                Scale();
            }
        }
        else
        {
            if (Input.GetKeyDown(KeyCode.G))
            {
                Scale();
            }
        }
    }

    private Scale()
    {
        scaleUp = !scaleUp;

        if (scaleCoroutine != null)
            StopCoroutine(scaleCoroutine);

        if (scaleUp)
        {
            scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, maxSize, duration));
        }
        else
        {
            scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, minSize, duration));
        }
    }

    private IEnumerator ScaleOverTime(GameObject targetObj, Vector3 toScale, float duration)
    {
        float counter = 0;
        Vector3 startScaleSize = targetObj.transform.localScale;

        coroutineIsRunning = true;

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

            if(counter > duration)
                coroutineIsRunning = false;

            yield return null;
        }
    }
}

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

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