为什么物体的缩放比例一直在不停地上下变化?

Why the scaling of the object is changing all the time nonstop up and down?

我想做的是当我在第一个脚本上按一次 F 并且确实显示对象时,然后启动协程以更改第二个对象上的对象比例。

问题是它不停地上下缩放对象,我希望它向上缩放一次,如果我再次按 F 不显示对象,它会缩小一次。

我按 F 的第一个脚本:

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

public class DroidMove : MonoBehaviour
{
    public GameObject droid;
    public ChangeScale changeScale;

    private float distance;
    private Camera cam; 

    private void Start()
    {
        cam = GetComponent<Camera>();
        distance = Vector3.Distance(cam.transform.position, droid.transform.position);
        droid.SetActive(false);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            droid.SetActive(!droid.activeInHierarchy);
            if (droid.activeInHierarchy == true)
            {
                changeScale.Scale();
            }
        }
    }
}

缩放对象的第二个脚本:

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

public class ChangeScale : MonoBehaviour
{
    public GameObject objectToScale;

    private float _currentScale = InitScale;
    private const float TargetScale = 1.1f;
    private const float InitScale = 0f;
    private const int FramesCount = 100;
    private const float AnimationTimeSeconds = 2;
    private float _deltaTime = AnimationTimeSeconds / FramesCount;
    private float _dx = (TargetScale - InitScale) / FramesCount;
    private bool _upScale = true;

    private IEnumerator Breath()
    {
        while (true)
        {
            while (_upScale)
            {
                _currentScale += _dx;
                if (_currentScale > TargetScale)
                {
                    _upScale = false;
                    _currentScale = TargetScale;
                }
                objectToScale.transform.localScale = Vector3.one * _currentScale;
                yield return new WaitForSeconds(_deltaTime);
            }

            while (!_upScale)
            {
                _currentScale -= _dx;
                if (_currentScale < InitScale)
                {
                    _upScale = true;
                    _currentScale = InitScale;
                }
                objectToScale.transform.localScale = Vector3.one * _currentScale;
                yield return new WaitForSeconds(_deltaTime);
            }
        }
    }

    public void Scale()
    {
        StartCoroutine(Breath());
    }
}

虽然我无法亲自测试,但我做了一些小改动,我相信应该会给您带来所需的行为。第一个脚本变为:

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

public class DroidMove : MonoBehaviour
{
    public GameObject droid;
    public ChangeScale changeScale;

    private float distance;
    private Camera cam; 

    private void Start()
    {
        cam = GetComponent<Camera>();
        distance = Vector3.Distance(cam.transform.position, droid.transform.position);
        droid.SetActive(false);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            changeScale.Scale();
        }
    }
}

第二个脚本变为:

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

public class ChangeScale : MonoBehaviour
{
    public GameObject objectToScale;

    private float _currentScale = InitScale;
    private const float TargetScale = 1.1f;
    private const float InitScale = 0f;
    private const int FramesCount = 100;
    private const float AnimationTimeSeconds = 2;
    private float _deltaTime = AnimationTimeSeconds / FramesCount;
    private float _dx = (TargetScale - InitScale) / FramesCount;
    private bool _upScaling = false;
    private bool _downScaling = false;

    private IEnumerator ScaleUp()
    {
        _upScaling = true;
        while (_upScaling)
        {
            _currentScale += _dx;
            if (_currentScale > TargetScale)
            {
                _upScaling = false;
                _currentScale = TargetScale;
            }
            objectToScale.transform.localScale = Vector3.one * _currentScale;
            yield return new WaitForSeconds(_deltaTime);
        }
    }

    private IEnumerator ScaleDown()
    {
        _downScaling = true;
        while (_downScaling)
        {
            _currentScale -= _dx;
            if (_currentScale < InitScale)
            {
                _downScaling = false;
                _currentScale = InitScale;
                droid.SetActive(false);
            }
            objectToScale.transform.localScale = Vector3.one * _currentScale;
            yield return new WaitForSeconds(_deltaTime);
        }
    }

    public void Scale(bool scaleUp)
    {
        if (!droid.activeInHierarchy) {
            droid.SetActive(true);
            StartCoroutine(ScaleUp());
        }

        if (_downScaling)
            StartCoroutine(ScaleUp());
        else
            StartCoroutine(ScaleDown());
    }
}

我基本上做了2个不同的套路;一种按比例放大,一种按比例缩小。当您按下 F 键时,我会调整对象的活动状态,并根据该状态调用正确的协程。