简体   繁体   English

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

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

What i want to do is when i press F once on the first script and it's true to display the object then start the coroutine to change the object scale on the second object. 我想做的是在第一个脚本上按F一次,然后显示对象,然后启动协程以更改第二个对象上的对象比例,这是正确的。

The problem is that it's scaling the object non stop up and down and i want it to scale it once up and if i will press F again to not displaying the object scale it back down once. 问题是它不停地缩放对象,我希望它一次缩放,如果我再按一次F则不显示该对象就不缩小了。

The first script where i press on 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();
            }
        }
    }
}

The second script that scale the object: 缩放对象的第二个脚本:

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

While I can't test it out myself, I made some minor changes that I believe should give you the desired behaviour. 虽然我自己无法测试,但我进行了一些小的更改,我相信这些更改可以为您带来理想的行为。 The first script becomes: 第一个脚本变为:

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

The second script becomes: 第二个脚本变为:

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

I basically made 2 different routines; 我基本上做了两个不同的例程。 one that scales up and one that scales down. 一种扩大规模,另一种缩小规模。 When you press F, I adapt the active state of the object, and depending on that state the right coroutine is called. 当您按F时,我将调整对象的活动状态,并根据该状态调用正确的协程。

暂无
暂无

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

相关问题 我如何使对象将不停地自动向上/向下缩放? - How can I make that the object will scale automatic up/down nonstop? 物体为什么不停地向上运动,运动完成后如何快速向前运动? - Why the object keep moving up nonstop and how to move it forward fast when it finished moving up? 为什么在检查器中更改运行时值时速度变量没有因素? 如何更新仓位,不停移动? - Why the speed variable have no factor when changing in the inspector the value in run time ? How to update positions and make nonstop moving? 在Unity中同时放大对象并将其移动到适当位置 - Scaling up an object and moving it into position at the same time in Unity 为什么当 object 到达目标时他停下来,然后向后移动,然后不停地打乒乓球? - Why when the object is reaching the target he stop and the move back and then make a ping pong nonstop? 如何在C#中的GraphicsPath对象上缩放矩形或形状时,如何修复图片框刷新时鼠标滚轮上下延迟? - How to fix the delay between mouse wheel up and down on the picture box refresh while scaling a rectangle or a shape on the GraphicsPath object in C#? IIS Express为什么总是关闭该应用程序并重新启动它? - Why does IIS Express shut down the Application and restart it all the time? 为什么我会收到不停的消息框? - Why am i getting nonstop message boxes? 是否有统一的方法将数组值缩小或缩小到给定范围 - is there unity method for scaling array values down or up to a given range 有时,缩小位图会生成更大的文件。为什么? - Sometimes, scaling down a bitmap generates a bigger file. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM