为什么对象的缩放总是在不停地上下变化?

时间:2017-12-22 21:34:11

标签: c# unity3d unity5

我想要做的是当我在第一个脚本上按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());
    }
}

1 个答案:

答案 0 :(得分:1)

虽然我无法自己测试,但我做了一些小改动,我认为应该给你想要的行为。第一个脚本变为:

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());
    }
}

我基本上做了两个不同的例程;一个放大,一个放大。按F时,我调整对象的活动状态,并根据该状态调用正确的协程。