[英]How can I make that when pressing F fast many time it will scale the object down/up smooth without waiting?
第一個腳本用於F按下並調用ScaleChange:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DroidMove : MonoBehaviour
{
public GameObject droid;
public ChangeScale changeScale;
private bool toDisplay = false;
private void Start()
{
droid.transform.localScale = new Vector3(0, 0, 0);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
toDisplay = !toDisplay;
changeScale.Scale(toDisplay);
}
}
}
第二個腳本進行縮放:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeScale : MonoBehaviour
{
public GameObject objectToScale;
private float _currentScale = InitScale;
private const float TargetScale = 0.7f;
private const float InitScale = 0f;
private const int FramesCount = 10;
private const float AnimationTimeSeconds = 0.1f;
private float _deltaTime = AnimationTimeSeconds / FramesCount;
private float _dx = (TargetScale - InitScale) / FramesCount;
private IEnumerator ScaleUp()
{
bool 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()
{
bool downscaling = true;
while (downscaling)
{
_currentScale -= _dx;
if (_currentScale < InitScale)
{
downscaling = false;
_currentScale = InitScale;
}
objectToScale.transform.localScale = Vector3.one * _currentScale;
yield return new WaitForSeconds(_deltaTime);
}
}
public void Scale(bool scaleUp)
{
if (scaleUp)
{
StartCoroutine(ScaleUp());
}
else
{
StartCoroutine(ScaleDown());
}
}
}
問題是,如果我按F並開始按比例放大,但還沒有完成按比例縮放,然后再次按F而不是按比例開始縮小,這就像暫停一樣;如果我按F則每次都會按比例放大。
但是我想做的是,當我第一次按F時開始按比例放大,如果在中間按比例縮放時我再次按F,則從當前縮放點開始按比例縮小,如果我按F則按向上和向下按平穩切換。
但是現在每次我按F多次都只是暫停。
就像我需要立即等待縮放先完成,然后再次按F一樣,否則它只會暫停它,並且我希望每次按F時都可以切換縮放。
您不需要使用多個功能來進行縮放。 一個應該做。 只需將比例值作為參數,然后重新使用該功能即可。 Min
和Max
比例應該是一個Vector3
而不是float
因為這是比例的表示方式,並且您希望覆蓋所有三個軸(x,y,z)。
如何解決按比例放大需要很長時間才能完成並且按下鍵時不切換方向的問題:
啟動協程時,請獲取正在運行的協程的引用。 在再次啟動協程之前,請使用舊參考停止舊協程。 就這么簡單。 與重寫現有代碼相比,重新編寫整個代碼要好得多。
以下是您的代碼的簡化版本。 確保設置minSize
, maxSize
和objectToScale
編者的變量。 如有任何疑問,請參見代碼注釋。
public GameObject objectToScale;
public Vector3 minSize;
public Vector3 maxSize;
private bool scaleUp = false;
private Coroutine scaleCoroutine;
// Use this for initialization
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
//Flip the scale direction when F key is pressed
scaleUp = !scaleUp;
//Stop old coroutine
if (scaleCoroutine != null)
StopCoroutine(scaleCoroutine);
//Scale up
if (scaleUp)
{
//Start new coroutine and scale up within 5 seconds and return the coroutine reference
scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, maxSize, 5f));
}
//Scale Down
else
{
//Start new coroutine and scale down within 5 seconds and return the coroutine reference
scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, minSize, 5f));
}
}
}
IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration)
{
float counter = 0;
//Get the current scale of the object to be scaled
Vector3 startScaleSize = targetObj.transform.localScale;
while (counter < duration)
{
counter += Time.deltaTime;
targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
yield return null;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.